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/

 

 

posted @   ®Geovin Du Dream Park™  阅读(39)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 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
点击右上角即可分享
微信分享提示