如何在 C# 中 List<string> 类型的公共属性中添加值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/620560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 10:35:16  来源:igfitidea点击:

How to add value in a public property of List<string> type in c#?

c#listcollections

提问by sunk

private List<string> _Baseline = new List<string>();

public List<string> Baseline 
{
    get { return _Baseline; }
    set { _Baseline = value; }
}

How can I set this property? It does not let me add using the add method; it throws an "object reference null" error.

如何设置此属性?它不允许我使用 add 方法添加;它会引发“对象引用为空”错误。

回答by Mehrdad Afshari

Do you initialize the class (using new) that holds this property before use?

您是否new在使用前初始化(使用)保存此属性的类?

There are two possible cases (assuming your code is in MyClassclass):

有两种可能的情况(假设您的代码在MyClass课堂上):

//External code:
MyClass x = new MyClass();
x.Baseline = null; // Somewhere it'll be set to null.
x.Baseline.Add("Something"); // NullReferenceException

Or:

或者:

//External code:
MyClass x = null; // Somewhere the class itself is set to null.
x.Baseline.Add("Something"); // NullReferenceException

回答by Diego Jancic

It should work if you did what you wrote here. I guess you are using generics, and I can't see them in your post.

如果你做了你在这里写的东西,它应该可以工作。我猜您正在使用泛型,而我在您的帖子中看不到它们。

If you have a complex expression, split it. For example, change ObjectA.Prop.Other.Xyz.Add(..) to:

如果您有一个复杂的表达式,请将其拆分。例如,将 ObjectA.Prop.Other.Xyz.Add(..) 更改为:

SomeClass a = ObjectA.Prop;
SomeClass2 b = a.Other;
SomeClass3 c = b.Xyz;
c.Add(...)

this way you will find quickly where the null reference is.

这样你会很快找到空引用的位置。

回答by Nick

You might need to use IList:

您可能需要使用 IList:

private IList<string> _Baseline = new List<string>();

public IList<string> Baseline 
{
    get { return _Baseline; }
    set { _Baseline = value; }
}

回答by Muad'Dib

There are 3 possibilities:

有3种可能:

  1. your list<string>is null
  2. the object containing your list<string>is null
  3. the item you are inserting is null
  1. list<string>是空的
  2. 包含您的对象list<string>为空
  3. 您插入的项目为空

1 is addressed when you assign a new List to it

1 在您为其分配新列表时得到解决

2 and 3 we can not ascertain from the code you post here.

2 和 3 我们无法从您在此处发布的代码中确定。

if you do not intend to allow assignment of a new list object outside of your class, then you do not, as noted elsewhere, need a setter. You can either remove it or declare it private or protected, like this....

如果你不打算允许在你的类之外分配一个新的列表对象,那么你不需要像其他地方提到的那样需要一个 setter。您可以将其删除或将其声明为私有或受保护,就像这样....

public List<string> Baseline 
{
    get { return _Baseline; }
    protected set { _Baseline = value; }
}

回答by Amay Kulkarni

i got the same error since the declaration was not proper:

由于声明不正确,我遇到了同样的错误:

private List<string> _Baseline;    - incorrect
private List<string> _Baseline = new List<string>();    - correct