为什么初始化程序不能处理返回 list的属性?

发布于 2024-11-16 01:55:36 字数 318 浏览 3 评论 0原文

找不到这个问题的答案。这一定是显而易见的,但仍然如此。

我尝试在这个简化的示例中使用初始值设定项:

    MyNode newNode = new MyNode 
    {
        NodeName = "newNode",
        Children.Add(/*smth*/) // mistake is here
    };

其中 Children 是此类的属性,它返回一个列表。在这里我遇到了一个错误,类似于“无效的初始化程序成员声明符”。

这里出了什么问题,如何初始化这些属性?预先非常感谢!

Couldn't find an answer to this question. It must be obvious, but still.

I try to use initializer in this simplified example:

    MyNode newNode = new MyNode 
    {
        NodeName = "newNode",
        Children.Add(/*smth*/) // mistake is here
    };

where Children is a property for this class, which returns a list. And here I come across a mistake, which goes like 'Invalid initializer member declarator'.

What is wrong here, and how do you initialize such properties? Thanks a lot in advance!

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

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

发布评论

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

评论(6

韶华倾负 2024-11-23 01:55:37

您不能在对象初始值设定项中调用类似的方法 - 您只能设置属性或字段,而不能调用方法。然而,在这种情况下,您可能仍然可以使用对象和集合初始值设定项语法:

MyNode newNode = new MyNode
{
    NodeName = "newNode",
    Children = { /* values */ }
};

请注意,这不会尝试为 Children 分配新值,它会调用 Children.Add(...),如下所示:

var tmp = new MyNode();
tmp.NodeName = "newNode":
tmp.Children.Add(value1);
tmp.Children.Add(value2);
...
MyNode newNode = tmp;

You can't call methods like that in object initializers - you can only set properties or fields, rather than call methods. However in this case you probably can still use object and collection initializer syntax:

MyNode newNode = new MyNode
{
    NodeName = "newNode",
    Children = { /* values */ }
};

Note that this won't try to assign a new value to Children, it will call Children.Add(...), like this:

var tmp = new MyNode();
tmp.NodeName = "newNode":
tmp.Children.Add(value1);
tmp.Children.Add(value2);
...
MyNode newNode = tmp;
闻呓 2024-11-23 01:55:37

这是因为children属性没有初始化

MyNode newNode = new MyNode 
    {
        NodeName = "newNode",
        Children = new List<T> (/*smth*/)
    };

It is because the children property is not initialized

MyNode newNode = new MyNode 
    {
        NodeName = "newNode",
        Children = new List<T> (/*smth*/)
    };
卸妝后依然美 2024-11-23 01:55:37

因为您正在执行一个方法,而不是赋值

Because you're executing a method, not assigning a value

却一份温柔 2024-11-23 01:55:37

字段初始值设定项语法只能用于设置字段和属性,不能用于调用方法。如果 ChildrenList,您也许可以通过这种方式完成它,还包括列表初始值设定项语法:

T myT = /* smth */

MyNode newNode = new MyNode 
{
    NodeName = "newNode",
    Children = new List<T> { myT }
};

The field initializer syntax can only be used for setting fields and properties, not for calling methods. If Children is List<T>, you might be able to accomplish it this way, by also including the list initializer syntax:

T myT = /* smth */

MyNode newNode = new MyNode 
{
    NodeName = "newNode",
    Children = new List<T> { myT }
};
狼亦尘 2024-11-23 01:55:37

以下不是在初始化程序中设置值:

Children.Add(/*smth*/) // mistake is here

它正在尝试访问字段的成员(也是尚未初始化的成员。)

The following is not setting a value in the initialiser:

Children.Add(/*smth*/) // mistake is here

It's trying to access a member of a field (a not-yet-initialised one, too.)

绝情姑娘 2024-11-23 01:55:37

初始化器只是初始化属性,而不是其他操作。

您并不是在尝试初始化 Children 列表,而是在尝试向其中添加一些内容。

Children = new List() 正在初始化它。

Initializers is just to initialize the properties, not other actions.

You are not trying to initialize the Children list, you are trying to add something to it.

Children = new List<smth>() is initializing it.

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