C#9 语法特性

embedded/2024/10/22 14:27:00/

theme: smartblue

一、 Record

1.1 基础用法

``` internal record Student { public int Id { get; set; } = 24;

public string Name { get; set; } = "xyy";

} ```

``` Person person = new Person("F","L"); Console.WriteLine($"fitst name {person.firstName}, last name {person.lastName}");

// 将 对象属性 赋予到 变量 person.Deconstruct(out string firstName, out string lastName); Console.WriteLine($"fitst name {firstName}, last name {lastName}"); ```

1.2 不可变性

默认 Record 属性不可变,Person 相当于 Person2 写法,只能在初始化值时设置内容

``` internal record Person(String firstName,string lastName);

internal record Person2() { public string fitstName { get; init; } = default!; public string lastName { get; init; } = default!; }; ``` 也可添加 set 允许,属性能够改变

internal record Person3() { public string fitstName { get; set; } = default!; public string lastName { get; set; } = default!; };

1.3 值相等性

若要依次比较两个 Record 对象是否相等,可直接使用=

``` Person person1 = new Person("F","L"); Person person2 = new Person("F", "L"); Console.WriteLine(person2 == person1); // true

Person person3 = new Person("L", "F"); Console.WriteLine(person3 == person2); // false ```

若要比较引用地址是否相等,可使用 ReferenceEquals 方法 Person person1 = new Person("F","L"); Person person2 = new Person("F", "L"); Console.WriteLine(ReferenceEquals(person1,person2)); // false

1.4 非破坏性变换

可以使用 with 关键词修改指定属性

Person person3 = person2 with { firstName="T"}; Console.WriteLine(person2); Console.WriteLine(person3); 执行结果如下:

Person { firstName = F, lastName = L } Person { firstName = T, lastName = L }

二、 仅限 Init 的资源库

添加 init 后,仅限于初始化时可以赋值

internal class Person2() { public string fitstName { get; init; } = default!; public string lastName { get; init; } = default!; };

三、 目标类型的 new 表达式

// 原写法 Student student1 = new Student(); // 修改后 Student student2 = new();

并且传入方法的参数为对象时,也可简写 ``` MyFunc(new());

void MyFunc(Student student){

}; ```


http://www.ppmy.cn/embedded/54225.html

相关文章

KVB App:金价看涨动能不足,关注美国PCE数据

当前市场状况 截至2024年6月28日,现货黄金价格震荡走弱,一度失守2320美元/盎司关口,目前交投于2322.65美元/盎司附近。KVB首席分析师Valeria Bednarik指出,黄金价格目前缺乏看涨动能,市场焦点转向美国个人消费支出(PC…

Double 4 VR虚拟情景智能互动系统在小语种专业课堂上的应用

随着科技的进步,越来越多的教育机构开始尝试使用虚拟现实技术来提高教学效果。Double 4 VR虚拟情景智能互动系统就是这样一款能够为小语种专业课堂带来革新性体验的教学工具。 一、模拟真实环境,增强学习体验 系统通过高度仿真的虚拟环境,为学…

Servlet工作原理

Servlet 工作原理 编写Servlet 创建servlet 创建一个MyServlet继承HttpServlet&#xff0c;重写doGet和doPost方法&#xff0c;也就是看请求的方式是get还是post&#xff0c;然后用不同的处理方式来处理请求&#xff0c; 2. 配置Servlet //添加参数 <servlet><se…

端口扫描攻击检测及防御方案

端口扫描数据一旦落入坏人之手&#xff0c;可能会成为更大规模恶意活动的一部分。因此&#xff0c;了解如何检测和防御端口扫描攻击至关重要。 端口扫描用于确定网络上的端口是否开放以接收来自其他设备的数据包&#xff0c;这有助于网络安全团队加强防御。但恶意行为者也可以…

[推荐]有安全一点的网贷大数据信用查询网站吗?

在互联网金融日益发展的今天&#xff0c;网贷大数据查询网站成为了许多人申贷前的必备工具。随着使用这些网站的人群越来越多&#xff0c;安全问题也逐渐浮出水面。最近&#xff0c;就有许多用户反馈自己的个人信息在网贷大数据查询网站上被泄露。为了解决这一问题&#xff0c;…

使用巴比达内网穿透搭建本地Web项目访问环境【免费方案】

你是否曾经遇到过这样的问题&#xff1a;你的服务器或者个人电脑处于内网环境中&#xff0c;无法直接通过公网IP进行访问&#xff1f;今天我要向大家推荐一款神器——巴比达内网穿透工具&#xff0c;帮助你轻松搭建SpringBoot的web外网访问环境&#xff01; 巴比达内网穿透是一…

vivado CLOCK_BUFFER_TYPE

时钟缓冲区类型 默认情况下&#xff0c;Vivado synthesis推断输入缓冲区和全局时钟缓冲区&#xff08;IBUF/BUFG&#xff09; 时钟端口的组合。但是&#xff0c;您可以使用IO_BUFFER_TYPE和 CLOCK_BUFFER_TYPE属性组合在一起&#xff0c;以指导Vivado合成工具更改 默认缓冲区类…

顺序表与链表学习笔记

顺序表及其结构定义 &#xff08;1&#xff09;结构定义 顺序存储&#xff1a; 顺序表的元素按顺序存储在一块连续的内存区域中&#xff0c;每个元素占用相同大小的存储空间。通过数组实现&#xff0c;每个元素可以通过下标快速访问。 存储密度高&#xff1a; 因为顺序表使用…