吸气剂自动属性只,可以设置,为什么呢? [英] Automated property with getter only, can be set, why?
问题描述
我创建了一个自动化的属性:
公众诠释美孚{搞定; }
这是唯一吸气剂。
但是当我建立一个构造函数,我可以改变的值:
公共MyClass的(字符串名称)
{
美孚= 5;
}
为什么可能的,尽管这让只?
这是一个新的C#6功能,消气仅自动性,也被称为关于Read自动属性初始化器只有属性在这里 MSDN杂志的一篇文章C#讨论:新的和改进的C#6.0 马克米氏。
只读场的二传手只是在构造函数访问,在其他所有方案领域仍然是只读的,表现为之前。
这是一个方便的语法来减少代码需要键入和删除需要显式声明私有模块级变量来保存价值的款额。
此功能被看作是重要的,因为在C#中引入自动实现的属性3,可变属性(那些具有getter和setter)已成为更快比写那些永恒不变的(那些只有一个getter),这意味着人们正在尝试使用可变属性来避免键入的代码,通常都需要只读属性支持字段。有一个在微软C#编程指南的相关章节的详细讨论。
的本博客文章,#1207 - C#6.0 - 自动属性初始值设定为只读属性肖恩·塞克斯顿拥有很好的解释和举例如下:
此前C#6.0中,如果你想要一个只读(immutable)的财产,你会
通常使用在
构造函数初始化,如下图所示只读支持领域。公共类犬
{
公共字符串名称{;组; }
// DogCreationTime是不可改变的
私人只读的DateTime creTime;
公众的DateTime DogCreationTime
{
{返回creTime; }
}
公狗(字符串名称)
{
名称=名称;
creTime = DateTime.Now;
}
}
在C#6.0中,你可以使用自动实现的属性实施
只读属性。通过使用自动属性
初始化做到这一点。其结果是比上面的例子中,其中
,我们必须明确地声明支持字段干净多了。公共类狗
{
公共字符串名称{;组; }
// DogCreationTime是不可改变的
公众的DateTime DogCreationTime {搞定; } = DateTime.Now;
公狗(字符串名称)
{
名称=名称;
}
}
块引用>
更多细节在的DOTNET罗斯林回购GitHub上:
自动性能,现在可以不用二传手声明
仅消气自动属性的支持字段是隐式
声明为只读(虽然这仅事项进行反思
的目的)。它可以通过在
属性的初始化被初始化为在上面的例子。此外,仅吸气属性可在声明类型的构造体分配到
,它使
值为被直接分配到基础字段
这是有关表达类型更简洁,但要注意,它也
去除了可变和
不变类型语言之间的一个重要区别:自动性也可用速记只有
您愿意让您的类可变的,所以诱惑,
默认的是伟大的。现在,只有消气自动属性时,
公平的竞争环境已经被夷为平地可变和不可变的。
块引用>I created an automated property:
public int Foo { get; }
This is getter only. But when I build a constructor, I can change the value:
public MyClass(string name) { Foo = 5; }
Why is it possible, even though this is get-only?
解决方案This is a new C# 6 feature, "Getter-only auto-properties", also known as "Auto-Property Initializers for Read-Only Properties" as discussed in this MSDN magazine article 'C# : The New and Improved C# 6.0' by Mark Michaelis.
The read-only field's setter is only accessible in the constructor, in all other scenarios the field is still read only and behaves as before.
This is a convenience syntax to reduce the amount of code you need to type and to remove the need to explicitly declare a private module level variable to hold the value.
This feature was seen as important as, since the introduction of Auto-Implemented Properties in C#3, mutable properties (those with a getter and setter) had become quicker to write than immutable ones (those with only a getter), meaning people were being tempted to use mutable properties to avoid having to type the code for a backing field usually required for read-only properties. There is more discussion of Auto-Implemented properties in the relevant section of the Microsoft C# Programming Guide.
This blog post, '#1,207 – C# 6.0 – Auto-Property Initializers for Read-Only Properties' by Sean Sexton Has a good explanation and example as follows:
Prior to C# 6.0, if you wanted a read-only (immutable) property, you’d typically use a read-only backing field that is initialized in the constructor, as shown below.
public class Dog { public string Name { get; set; } // DogCreationTime is immutable private readonly DateTime creTime; public DateTime DogCreationTime { get { return creTime; } } public Dog(string name) { Name = name; creTime = DateTime.Now; } }
In C# 6.0, you can use auto-implemented properties to implement a read-only property. You do this by using an auto-property initializer. The result is much cleaner than the above example, where we had to explicitly declare a backing field.
public class Dog { public string Name { get; set; } // DogCreationTime is immutable public DateTime DogCreationTime { get; } = DateTime.Now; public Dog(string name) { Name = name; } }
More details can also be found in the dotnet Roslyn repo on GitHub:
Auto-properties can now be declared without a setter.
The backing field of a getter-only auto-property is implicitly declared as readonly (though this matters only for reflection purposes). It can be initialized through an initializer on the property as in the example above. Also, a getter-only property can be assigned to in the declaring type’s constructor body, which causes the value to be assigned directly to the underlying field:
This is about expressing types more concisely, but note that it also removes an important difference in the language between mutable and immutable types: auto-properties were a shorthand available only if you were willing to make your class mutable, and so the temptation to default to that was great. Now, with getter-only auto-properties, the playing field has been leveled between mutable and immutable.
这篇关于吸气剂自动属性只,可以设置,为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!