CSharp: Classes, Structures, And Records in c# 10(donet 6)
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | #region Record Types Record Class in C# 10 /// <summary> /// Positional parameters syntax /// 位置参数的语法 /// </summary> /// <param name="EmployeeId"></param> /// <param name="FirstName"></param> /// <param name="LastName"></param> public record Employee( string EmployeeId, string FirstName, string LastName); /// <summary> /// Standard property syntax /// 标准属性的语法 /// </summary> public record DuEmployee { public string EmployeeId { get ; init; } public string FirstName { get ; init; } public string LastName { get ; init; } } #endregion #region Record Structures /// <summary> /// Positional parameters syntax /// 位置参数的语法 /// </summary> /// <param name="EmployeeId"></param> /// <param name="FirstName"></param> /// <param name="LastName"></param> public readonly record struct GeovinEmployee( string EmployeeId, string FirstName, string LastName); /// <summary> /// Standard property syntax /// 标准属性的语法 /// </summary> public readonly record struct GeovinDuEmployee { public string EmployeeId { get ; init; } public string FirstName { get ; init; } public string LastName { get ; init; } } #endregion #region Record Class /// <summary> /// Positional parameters syntax /// 位置参数的语法 /// </summary> /// <param name="EmployeeId"></param> /// <param name="FirstName"></param> /// <param name="LastName"></param> public record TuEmployee( string EmployeeId, string FirstName, string LastName) { public string Designation { get ; set ; } } /// <summary> /// Standard property syntax /// 标准属性的语法 /// </summary> public record TujuEmployee { public string EmployeeId { get ; init; } public string FirstName { get ; init; } public string LastName { get ; init; } public string Designation { get ; set ; } } #endregion #region The record structs /// <summary> /// Positional parameters syntax /// 位置参数的语法 /// </summary> /// <param name="EmployeeId"></param> /// <param name="FirstName"></param> /// <param name="LastName"></param> /// <param name="Designation"></param> public record struct TujuwenEmployee( string EmployeeId, string FirstName, string LastName, string Designation); /// <summary> /// Standard property syntax /// 标准属性的语法 /// </summary> public record struct LukEmployee { public string EmployeeId { get ; init; } public string FirstName { get ; init; } public string LastName { get ; init; } public string Designation { get ; set ; } } #endregion |
调用:
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 | Employee employee = new ( "E001" , "Geovin" , "Du" ); Console.WriteLine(employee); DuEmployee duemployee = new DuEmployee() { EmployeeId = "E001" , FirstName = "聚文" , LastName = "涂" }; Console.WriteLine(duemployee); GeovinEmployee geovinemployee = new ( "E001" , "Geovin" , "Du" ); Console.WriteLine(geovinemployee); GeovinDuEmployee geovinduemployess = new GeovinDuEmployee() { EmployeeId = "E001" , FirstName = "聚文" , LastName = "涂" }; Console.WriteLine(geovinduemployess); TuEmployee tuemployee = new ( "E001" , "聚文" , "涂" ) { Designation = "软件工程师" }; Console.WriteLine(tuemployee); TujuEmployee tujuemployee = new TujuEmployee() { EmployeeId = "E001" , FirstName = "Geovin" , LastName = "Du" , Designation = "Software Engineer" }; Console.WriteLine(tujuemployee); TujuwenEmployee tujuwenemployee = new ( "E001" , "Geovin" , "Du" , "软件工程师" ); Console.WriteLine(tujuwenemployee); LukEmployee lukemployee = new LukEmployee { EmployeeId = "E001" , FirstName = "聚文" , LastName = "涂" , Designation = "Software Engineer" }; Console.WriteLine(lukemployee); |
输出:
1 2 3 4 5 6 7 8 | Employee { EmployeeId = E001, FirstName = Geovin, LastName = Du } DuEmployee { EmployeeId = E001, FirstName = 聚文, LastName = 涂 } GeovinEmployee { EmployeeId = E001, FirstName = Geovin, LastName = Du } GeovinDuEmployee { EmployeeId = E001, FirstName = 聚文, LastName = 涂 } TuEmployee { EmployeeId = E001, FirstName = 聚文, LastName = 涂, Designation = 软件工程师 } TujuEmployee { EmployeeId = E001, FirstName = Geovin, LastName = Du, Designation = Software Engineer } TujuwenEmployee { EmployeeId = E001, FirstName = Geovin, LastName = Du, Designation = 软件工程师 } LukEmployee { EmployeeId = E001, FirstName = 聚文, LastName = 涂, Designation = Software Engineer } |
https://bulisor.github.io/Experiments/
https://github.com/BabylonJS/Babylon.js
https://github.com/Bulisor/SmarTeam3D/
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/record-structs
https://github.com/falberthen/NET6.Features
https://github.com/mganss/HtmlSanitizer
https://www.c-sharpcorner.com/article/classes-structures-and-records-oh-my/
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
CSharp code
标签:
Record
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!