C# 无法将项添加到在其他类中初始化的列表中
朋友们,我想知道为什么下面的代码适用于C# 无法将项添加到在其他类中初始化的列表中,c#,list,initialization,C#,List,Initialization,朋友们,我想知道为什么下面的代码适用于List,而不适用于List。当我在属性的getter中初始化列表时,它就不工作了。如果我在构造函数中初始化,它就会工作,如果我在调用类中创建列表,它就会工作 public class MyClass { private List<int> _ints; private static List<string> _strings; public MyClass() { _ints = n
List
,而不适用于List
。当我在属性的getter中初始化列表时,它就不工作了。如果我在构造函数中初始化,它就会工作,如果我在调用类中创建列表,它就会工作
public class MyClass
{
private List<int> _ints;
private static List<string> _strings;
public MyClass()
{
_ints = new List<int>();
}
public List<int> Ints
{
get
{
return _ints;
}
set
{
_ints = value;
}
}
public List<string> Strings
{
get
{
return _strings ?? new List<string>();
}
}
}
class Program
{
static void Main(string[] args)
{
MyClass mc = new MyClass();
// This works
// mc.Ints = new List<int>();
// This works
mc.Ints.Add(1);
// This does not
mc.Strings.Add("Krishna");
}
}
公共类MyClass
{
私人名单;
私有静态列表_字符串;
公共MyClass()
{
_ints=新列表();
}
公共列表Ints
{
得到
{
返回整数;
}
设置
{
_ints=数值;
}
}
公共列表字符串
{
得到
{
返回_字符串??新列表();
}
}
}
班级计划
{
静态void Main(字符串[]参数)
{
MyClass mc=新的MyClass();
//这很有效
//mc.Ints=新列表();
//这很有效
mc.Ints.Add(1);
//这是不可能的
mc.Strings.Add(“奎师那”);
}
}
您没有在getter上初始化变量,如果变量为null,则创建一个新列表,但不存储对它的引用
将代码更改为:
public List<string> Strings
{
get
{
if(_strings == null)
_strings = new List<string>();
return _strings;
}
}
公共列表字符串
{
得到
{
如果(_strings==null)
_字符串=新列表();
返回_字符串;
}
}
您没有在getter上初始化变量,如果变量为null,则创建一个新列表,但不存储对它的引用
将代码更改为:
public List<string> Strings
{
get
{
if(_strings == null)
_strings = new List<string>();
return _strings;
}
}
公共列表字符串
{
得到
{
如果(_strings==null)
_字符串=新列表();
返回_字符串;
}
}
因为List
在类的构造函数上初始化,而asList
没有初始化。尝试:
public MyClass()
{
_ints = new List<int>();
_strings=new List<string>();
}
publicmyclass()
{
_ints=新列表();
_字符串=新列表();
}
因为List
在类的构造函数上初始化,而asList
没有初始化。尝试:
public MyClass()
{
_ints = new List<int>();
_strings=new List<string>();
}
publicmyclass()
{
_ints=新列表();
_字符串=新列表();
}
您的“\u字符串”列表始终为空。如果调用“Strings”属性的“get”,则始终创建并返回新的列表实例
get
{
// if '_strings' is null, then create new instance of list
return _strings ?? new List<string>();
}
get
{
//如果“\u strings”为空,则创建列表的新实例
返回_字符串??新列表();
}
您需要使用以下选项:
get
{
// If '_strings' is null, then create new instance of list and assign new instance to '_strings'
return _strings ?? (_strings = new List<string>());
}
get
{
//如果“\u strings”为空,则创建列表的新实例并将新实例分配给“\u strings”
返回_字符串??(_字符串=新列表());
}
您的“\u字符串”列表始终为空。如果调用“Strings”属性的“get”,则始终创建并返回新的列表实例
get
{
// if '_strings' is null, then create new instance of list
return _strings ?? new List<string>();
}
get
{
//如果“\u strings”为空,则创建列表的新实例
返回_字符串??新列表();
}
您需要使用以下选项:
get
{
// If '_strings' is null, then create new instance of list and assign new instance to '_strings'
return _strings ?? (_strings = new List<string>());
}
get
{
//如果“\u strings”为空,则创建列表的新实例并将新实例分配给“\u strings”
返回_字符串??(_字符串=新列表());
}
此mc.Strings.Add(“奎师那”)
不起作用,因为每次调用mc.Strings
时都会返回新的列表。要更详细地解释它,请执行以下操作:
MyClass mc = new MyClass(); // create new instance of MyClass and store it in the "mc" variable
mc // Direct call to the instance
.Strings // Direct call to MyClass Strings property
// inside Strings property :
// return _strings ?? new List<string>();
// meaning that if "_strings" member field is null
// return new instance of that property's type
.Add("abc"); // adds "abc" to the "forgotten" instance of List<string>
MyClass mc=newmyclass();//创建MyClass的新实例并将其存储在“mc”变量中
mc//直接调用实例
.Strings//直接调用MyClass Strings属性
//内部字符串属性:
//返回字符串??新列表();
//这意味着如果“_strings”成员字段为空
//返回该属性类型的新实例
.添加(“abc”);//将“abc”添加到列表的“遗忘”实例中
简单地说,你所做的和你所说的一样:
new List<string>().Add("abc");
newlist().Add(“abc”);
要修复此问题,您可以使用一个衬里(您显然已经尝试过),如下所示:
return _strings ?? ( _strings = new List<string>() );
返回\u字符串??(_strings=newlist());
或使用if语句:
if(_strings == null)
_strings = new List<string>();
return _strings;
if(_strings==null)
_字符串=新列表();
返回_字符串;
此mc.Strings.Add(“奎师那”)
不起作用,因为每次调用mc.Strings
时都会返回新的列表。要更详细地解释它,请执行以下操作:
MyClass mc = new MyClass(); // create new instance of MyClass and store it in the "mc" variable
mc // Direct call to the instance
.Strings // Direct call to MyClass Strings property
// inside Strings property :
// return _strings ?? new List<string>();
// meaning that if "_strings" member field is null
// return new instance of that property's type
.Add("abc"); // adds "abc" to the "forgotten" instance of List<string>
MyClass mc=newmyclass();//创建MyClass的新实例并将其存储在“mc”变量中
mc//直接调用实例
.Strings//直接调用MyClass Strings属性
//内部字符串属性:
//返回字符串??新列表();
//这意味着如果“_strings”成员字段为空
//返回该属性类型的新实例
.添加(“abc”);//将“abc”添加到列表的“遗忘”实例中
简单地说,你所做的和你所说的一样:
new List<string>().Add("abc");
newlist().Add(“abc”);
要修复此问题,您可以使用一个衬里(您显然已经尝试过),如下所示:
return _strings ?? ( _strings = new List<string>() );
返回\u字符串??(_strings=newlist());
或使用if语句:
if(_strings == null)
_strings = new List<string>();
return _strings;
if(_strings==null)
_字符串=新列表();
返回_字符串;
Gusman答案的内联变体,使用C#6.0的一些优秀功能
公共列表字符串=>\u字符串??(_strings=newlist());
Gusman答案的内联变体,使用C#6.0的一些优秀功能
公共列表字符串=>\u字符串??(_strings=newlist());
仅供参考,为了完全理解问题,您可以在这行上设置一个断点:
return _strings ?? new List<string>();
返回\u字符串??新列表();
你会看到\u字符串总是空的,你需要初始化它,因为上面的答案告诉你:)仅供参考,为了完全理解这个问题,你可以在这行上设置一个断点:
return _strings ?? new List<string>();
返回\u字符串??新列表();
您可能会看到\u strings
始终为空,您需要根据上面的答案对其进行初始化:)您从未将\u strings
分配给