简体   繁体   English

表达式体成员不会创建新实例?

[英]Expression-bodied members doesn't create new instances?

I'm curiuous why Expression-bodied properties doesn't create persisant objects.我很好奇为什么 Expression-bodied 属性不会创建持久对象。

public List<string> Pages { get; } = new List<string>();

Does create a persistant isntance of List<string> , just like always.像往常一样创建List<string>的持久性实例。

But

public List<string> Pages => new List<string>();

Somehow this does craete a new instance, but seems to be volatile.不知何故,这确实创造了一个新的实例,但似乎是不稳定的。

Even when adding a new string to Pages won't add it.即使将新字符串添加到Pages也不会添加它。

There's no runtime- nor compile-time error, but I think there should be at least a warning.没有运行时或编译时错误,但我认为至少应该有一个警告。 It took me quite a while to figure it out.我花了很长时间才弄清楚。

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/expression-bodied-members https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/expression-bodied-members

Is a bit odd documented.记录在案有点奇怪。

Properties noted with { get; }{ get; }标记的属性{ get; } have an internal field that stores the value. { get; }有一个存储值的内部字段。 However, the second example which is expression-bodied, declares the creation of the list as the getter, which means it will be ran every time you access it, and every time it creates a new list.但是,第二个表达式主体的示例将列表的创建声明为 getter,这意味着每次访问它时都会运行它,并且每次它创建一个新列表时都会运行它。

As noted in comments, it is the same as get { return new List<string>(); }如评论中所述,它与get { return new List<string>(); }相同。 get { return new List<string>(); } , to explain it in a different way. get { return new List<string>(); } ,以不同的方式解释它。

To change this behavior, you need to have an internal field which would store the List instance and the expression-bodied member to return it.要更改此行为,您需要有一个内部字段来存储 List 实例和表达式主体成员以返回它。

Following the link in the documentation you linked, we have read-only properties :按照您链接的文档中的链接,我们有只读属性

Starting with C# 6, you can use expression body definition to implement a read-only property.从 C# 6 开始,您可以使用表达式主体定义来实现只读属性。 To do that, use the following syntax:为此,请使用以下语法:

 PropertyType PropertyName => expression;

The following example defines a Location class whose read-only Name property is implemented as an expression body definition that returns the value of the private locationName field:以下示例定义了一个 Location 类,其只读 Name 属性实现为表达式主体定义,该定义返回私有 locationName 字段的值:

 public class Location { private string locationName; public Location(string name) { locationName = name; } public string Name => locationName; }

(Emphasis mine) (强调我的)

To rephrase it: when you access the Name property, the value held by locationName is returned.换个说法:当您访问Name属性时,将返回locationName保存的值。 If the value of locationName changes, and you access Name again, you get the new value of locationName .如果locationName的值发生变化,并且您再次访问Name ,您将获得locationName的新值。

It's equivalent to:它相当于:

public string Name
{
    get => locationName;
}

Or或者

public string Name
{
    get { return locationName; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM