C产品在市场上有哪些独特优势?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1047个文字,预计阅读时间需要5分钟。
翻译自John Demetriou 2018年4月8日的文章《》
翻译自 John Demetriou 2018年4月8日 的文章 《C# 7.2 – Let's Talk About Readonly Structs》[1]
在本文中,我们来聊一聊从 C# 7.2 开始出现的一个特性 readonly struct。
任一结构体都可以有公共属性、私有属性访问器等等。我们从以下结构体示例来开始讨论:
public struct Person { public string Name { get; set; } public string Surname { get; set; } public int Age { get; set; } public Person(string name, string surname, int age) { Name = name; Surname = surname; Age = age; } public void Replace(Person other) { this = other; } }
如您所见,所有属性都可以公开访问和修改。更糟糕的是,我们甚至可以访问 this (通过调用 Replace 方法),将其更改为同一结构体类型的另一个实例。
这就是 readonly 关键字出现的原因。
本文共计1047个文字,预计阅读时间需要5分钟。
翻译自John Demetriou 2018年4月8日的文章《》
翻译自 John Demetriou 2018年4月8日 的文章 《C# 7.2 – Let's Talk About Readonly Structs》[1]
在本文中,我们来聊一聊从 C# 7.2 开始出现的一个特性 readonly struct。
任一结构体都可以有公共属性、私有属性访问器等等。我们从以下结构体示例来开始讨论:
public struct Person { public string Name { get; set; } public string Surname { get; set; } public int Age { get; set; } public Person(string name, string surname, int age) { Name = name; Surname = surname; Age = age; } public void Replace(Person other) { this = other; } }
如您所见,所有属性都可以公开访问和修改。更糟糕的是,我们甚至可以访问 this (通过调用 Replace 方法),将其更改为同一结构体类型的另一个实例。
这就是 readonly 关键字出现的原因。

