首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >CSharp: Classes, Structures, And Records in c# 10(donet 6)

CSharp: Classes, Structures, And Records in c# 10(donet 6)

作者头像
geovindu
发布2026-06-18 13:24:12
发布2026-06-18 13:24:12
730
举报
代码语言:javascript
复制
#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

调用:

代码语言:javascript
复制
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);

输出:

代码语言:javascript
复制
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 }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2026-06-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档