英文:
What exactly are the "auto-implemented properties" introduced in C# 6.0?
问题
C# 6.0引入了"auto-implemented properties"(自动实现属性)这一新特性,该特性允许你像示例中那样轻松地定义属性,无需明确实现其get和set方法。在C# 5.0版本中,你可以使用此特性,但不能使用其他在C# 6.0中引入的功能,如null合并运算符和字符串插值。
在你提供的示例中,Foo
类中的Bar
属性就是auto-implemented property的示例,它在C# 6.0中引入,但在C# 5.0中也是有效的。因此,你可以使用auto-implemented properties,但不能使用null合并运算符和字符串插值等其他C# 6.0功能,因为它们需要C# 6.0或更高版本才能支持。
总结:auto-implemented properties是C# 6.0引入的功能,但在C# 5.0中也可用。其他功能,如null合并运算符和字符串插值,只能在C# 6.0或更高版本中使用。
英文:
Microsoft's version history page mentions "auto-implemented properties" as one of the new features added to C# in version 6.
Their page on auto-implemented properties gives the following code example:
public class Customer
{
// Auto-implemented properties for trivial get and set
public double TotalPurchases { get; set; }
public string Name { get; set; }
public int CustomerId { get; set; }
...
}
I have just encountered some very old code running on C# version 5.0. I can verify it is running on version 5 because it fails to build if I attempt to add in string interpolation ($"{x}"
) or null the coalescing operator (x?.y
), both other features added in 6.0.
However, if I add code similar to the example above, it works. In particular, this complete program works as expected, and prints out "other text2".
public class Program
{
static void Main(string[] args)
{
var x = new Foo
{
Bar = "text"
};
x.Bar = "other text";
var y = x.Bar + "2";
Console.WriteLine(y);
}
}
public class Foo
{
public string Bar { get; set; }
}
So, what exactly is the feature that you have access to in C# 6.0 that is lacking in 5.0, which the version history refers to as "auto-implemented properties"? Or, alternatively, is there some reason that despite running with a LangVersion
of 5.0
, I would be able to use auto-implemented properties but not null coalescing, string interpolation, and other features from C# 6?
答案1
得分: 2
以下是要翻译的内容:
Microsoft的版本历史页面提到“自动实现属性”是C# 6版本中新增的新功能。
但实际上并非如此。新增的是“自动属性初始化器”。自动实现属性(或简称自动属性)是在C# 3中新增的。
在自动属性编程指南页面开头显示的示例未使用自动属性初始化器,这就是为什么您仍然可以在C# 5中编译该代码的原因。
然而,在该页面的后面部分提到:
您可以类似于字段初始化自动实现属性:
public string FirstName { get; set; } = "Jane";
这是在C# 6中新增的。在此之前,如果要将自动属性初始化为自定义值,您需要在构造函数中执行:
public class Person {
public string FirstName { get; set; }
public MyClass() {
FirstName = "John";
}
}
英文:
> Microsoft's version history page mentions "auto-implemented properties" as one of the new features added to C# in version 6.
That's not what it says. It was "auto-property initialisers" that was added. Auto-implemented properties (or auto-properties for short) were added in C# 3.
The example shown at the start of the auto-properties programming guide page does not use auto-property initialisers, which is why you can still compile the code in C# 5.
However, further down that page, it says:
> You can initialize auto-implemented properties similarly to fields:
>
> public string FirstName { get; set; } = "Jane";
That was new in C# 6. Before, you had to do it in a constructor if you wanted to initialise auto-properties to a custom value:
public class Person {
public string FirstName { get; set; }
public MyClass() {
FirstName = "John";
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论