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); |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
CSharp code
标签:
元组
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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# 验证中国电话号码,电子邮件,网址,身份证号码等等