具有自动属性的集合初始值设定项

发布于 2024-11-02 22:07:47 字数 499 浏览 0 评论 0原文

有没有办法在使用自动属性的同时使用集合初始值设定项?

// Uses collection initializer but not automatic properties
private List<int> _numbers = new List<int>();
public List<int> Numbers
{
    get { return _numbers; }
    set { _numbers = value; }
}


// Uses automatic properties but not collection initializer
public List<int> Numbers { get; set; }


// Is there some way to do something like this??
public List<int> Numbers { get; set; } = new List<int>();

Is there a way to use a collection initializer when also using automatic properties?

// Uses collection initializer but not automatic properties
private List<int> _numbers = new List<int>();
public List<int> Numbers
{
    get { return _numbers; }
    set { _numbers = value; }
}


// Uses automatic properties but not collection initializer
public List<int> Numbers { get; set; }


// Is there some way to do something like this??
public List<int> Numbers { get; set; } = new List<int>();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

指尖微凉心微凉 2024-11-09 22:07:47

不,基本上。您必须在构造函数中初始化集合。老实说,无论如何,可设置的集合都不是一个好主意。我实际上会使用(更改您的第一个版本,删除 set):

private readonly List<int> _numbers = new List<int>();
public List<int> Numbers { get { return _numbers; } }

或者如果我想推迟构建直到第一次访问:

private List<int> _numbers;
public List<int> Numbers {
    get { return _numbers ?? (_numbers = new List<int>()); }
}

No, basically. You would have to initialize the collection in the constructor. To be honest, a settable collection is rarely a good idea anyway; I would actually use just (changing your first version, removing the set):

private readonly List<int> _numbers = new List<int>();
public List<int> Numbers { get { return _numbers; } }

or if I want to defer construction until the first access:

private List<int> _numbers;
public List<int> Numbers {
    get { return _numbers ?? (_numbers = new List<int>()); }
}
别理我 2024-11-09 22:07:47

// 有什么方法可以做这样的事情吗?

公共列表;数字 { 得到;放; } = new List();

不行。您必须在显式定义的构造函数中进行初始化,这里没有可应用的字段初始化技巧。

此外,这与集合初始化器无关。您也无法

public object Foo { get; set; }

在构造函数之外进行初始化。

// Is there some way to do something like this??

public List<int> Numbers { get; set; } = new List<int>();

No. You have to initialize in an explicitly-defined constructor, there are no field-initialization tricks to apply here.

Also, this has nothing to do with collection intializers. You also can't initialize

public object Foo { get; set; }

outside of a constructor.

等风来 2024-11-09 22:07:47

显然你的声音被听到了。
我刚刚偶然发现它第一次在新的代码库中使用。

https:// learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties

“在 C# 6 及更高版本中,您可以初始化自动实现的属性
与字段类似:"

public string FirstName { get; set; } = "Jane";  

apparently your voice was heard.
I just stumbled across this being used for the first time in a new codebase.

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties

"In C# 6 and later, you can initialize auto-implemented properties
similarly to fields:"

public string FirstName { get; set; } = "Jane";  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文