C#——属性init访问器方法
1.C# 中数字常见操作及其取值范围2.C# 泛型列表类型管理数据集合3.C# 通过Queue类来实现队列的先进先出4.C# 非泛型集合的简单迭代5.C# 语言集成查询LINQ6.委托简单实现7..net 和.net framework区别8.C#-随笔杂记9.wpf-TextBlock文本内容自动换行10.C# 非引用类型初始化为空11.C#------------await12.C#------新特性之??、?、??= 使用13.C#------LINQ查询(一)14.C#——LINQ to XML(创建 XML 树)15.C#——LINQ to XML(内容快速查找)16.C#——LINQ to XML(使用 Descendants 方法查找单个子代)17.C#——fixed用法18.C#——匿名类型
19.C#——属性init访问器方法
20.C#——自动实现的属性21.C#——对象初始值设定项22.Assembly.CreateInstance 方法和Activator.CreateInstance 方法(C#)23.DragDrop.DoDragDrop(DependencyObject, Object, DragDropEffects) 方法——控件拖动方法24.通讯过程中16进制字符和byte[]/十进制和16进制转换(一)25.C# Modbus Tcp 实现(二)26.C# Modbus Tcp实现(一)27.C# 进制变换过程中左侧填充0实现28.DLR(动态语言运行时)(一)29.wpf textbox中UpdateSourceTrigger=PropertyChangedinit关键字:
1.init在属性或索引器中定义访问器方法
2.仅在对象构造期间为属性或索引器元素赋值
3.init强制实施不可变性(对象一旦初始化,将无法更改)
4.如下同时定义get和init访问器
class Person_InitExample { private int _yearOfBirth; public int YearOfBirth { get { return _yearOfBirth; } init { _yearOfBirth = value; } } }
var john = new Person_InitExample { YearOfBirth = 1984 }; john.YearOfBirth = 1926; //不起作用,init关键字只允许在初始化对象过程中指定值
5.init访问器不强制调用方法设置属性(允许调用方使用对象初始化设定项,同时禁止以后的修改)
class Person_InitExampleNullability { private int? _yearOfBirth; //定义一个以可为空的值类型作为支持字段的仅init属性 public int? YearOfBirth { get => _yearOfBirth; init => _yearOfBirth = value; } }
6.若要强制调用方设置初始化非null值,添加required修饰符
//强制调用方设置初始非 null 值,可添加 required 修饰符 class Person_InitExampleNonNull { private int _yearOfBirth; public required int YearOfBirth { get => _yearOfBirth; init => _yearOfBirth = value; } }
7.init访问器用作表达式主题成员
class Person_InitExampleExpressionBodied { private int _yearOfBirth; public int YearOfBirth { get => _yearOfBirth; init => _yearOfBirth = value; } }
8.init访问器在自动实现的属性中使用
class Person_InitExampleAutoProperty { public int YearOfBirth { get; init; } }
9.private set属性 只读 属性和init属性区别
private set 版本和 read only 版本都需要调用方使用添加的构造函数来设置 name 属性。 通过 private set
版本,人员可在构造实例后更改其名称。 init
版本不需要构造函数。 调用方可使用对象初始值设定项初始化属性:
class PersonPrivateSet { public string FirstName { get; private set; } public string LastName { get; private set; } public PersonPrivateSet(string first, string last) => (FirstName, LastName) = (first, last); public void ChangeName(string first, string last) => (FirstName, LastName) = (first, last); } class PersonReadOnly { public string FirstName { get; } public string LastName { get; } public PersonReadOnly(string first, string last) => (FirstName, LastName) = (first, last); } class PersonInit { public string FirstName { get; init; } public string LastName { get; init; } } PersonPrivateSet personPrivateSet = new("Bill", "Gates"); PersonReadOnly personReadOnly = new("Bill", "Gates"); PersonInit personInit = new() { FirstName = "Bill", LastName = "Gates" };
------------------------------------
承接
**视觉检测软件开发及调试
**工业软件开发
**上位机软件开发
wechat:luoran2024
qq:565934058
email:taoyuansu@qq.com
海量教育资源及影视资源下载
微信公众号:EFun科技
------------------------------------
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!