CSharp: Tuples

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/// <summary>
/// 使用元组(Tuples或ValueTuple
/// </summary>
/// <param name="id"></param>
/// <param name="name"></param>
/// <returns></returns>
public static (int, string) GetInfo(int gid,string gname)
{
    int id = gid;
    string name = gname;
    return (id, name);
}
/// <summary>
/// AnonymousType
/// </summary>
/// <returns></returns>
public static dynamic GetValuesAsDynamic(int gid, string gname)
{
    return new { id = gid, name =gname };
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//元组Tuples
var person = ("Geovin Du", 30, "Soft Developer");
Console.WriteLine(person.Item1);
 
Tuple<string, int, string> persons = new Tuple<string, int, string>("Geovin Du", 30, "Soft Developer");
Console.WriteLine($"Name: {persons.Item1}, Age: {persons.Item2}, Occupation: {persons.Item3}");
 
//使用元组(Tuples或ValueTuple
var (gid, gname) = SortExample.GetInfo(20, "geovin");
Console.WriteLine($" id:{gid},name:{gname}");
 
dynamic dynamicResult=SortExample.GetValuesAsDynamic(20, "geovindu");
Console.WriteLine(dynamicResult);
Console.WriteLine(dynamicResult.GetType().GetProperty("id").GetValue(dynamicResult, null));
Console.WriteLine(dynamicResult.GetType().GetProperty("name").GetValue(dynamicResult, null));
Console.WriteLine(dynamicResult.GetType());
 
Stopwatch stopwatch = Stopwatch.StartNew();
 
Console.WriteLine(typeof(Person));
foreach (var propertyInfo in typeof(Person).GetProperties())
{
    Console.WriteLine(propertyInfo.Name);
}
 
 
var school = new
{
    Address = "Orlando",
    Contact = 1200,
    Employee = new { Id = 3, Name = "Tina" }
};
 
Console.WriteLine(school.Address);
 
 
dynamicResult = SortExample.GetValuesAsDynamic(30, "geovindu");
 
//AnonymousType another = new AnonymousType(something);
if (dynamicResult!=null)
{
 
    var v = dynamicResult;
    // v.id = 3;
    // v.name = "geovindu";
    Console.WriteLine(v);
   // object o = (object)v;
    //Console.WriteLine(o.ToString());
 
    //var d= Newtonsoft.Json.JsonConvert.DeserializeObject(dynamicResult.ToString());
    var d = JsonConvert.DeserializeObject<dynamic>("{ id: 30, name: \"geovindu\" }");
 
 
}
 
    stopwatch.Stop();
Console.WriteLine("Delegate: {0}ms", stopwatch.ElapsedMilliseconds);

  

posted @   ®Geovin Du Dream Park™  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2019-05-22 Python: simple drawings
2015-05-22 sql: Oracle 11g create table, function,trigger, sequence
2013-05-22 display month as a calendar using sql
2012-05-22 C# 輸入法
2011-05-22 C# 验证中国电话号码,电子邮件,网址,身份证号码等等
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示