这是一个错误还是我解释'??'-操作员错了?查看下面的 get 属性和评论。
我正在使用 C# .NET 3.5
private List<MyType> _myTypeList;
private List<MyType> MyTypeList
{
get
{
//The two code lines below should each behave as the three under, but they don't?
//The ones uncommented are working, the commented result in my list always returning empty (newly created I suppose).
//return _myTypeList ?? new List<MyType>();
//return _myTypeList == null ? new List<MyType>() : _myTypeList;
if (_myTypeList == null)
_myTypeList = new List<MyType>();
return _myTypeList;
}
}
编辑:对于刚问到这个问题时看到这个问题的每个人,很抱歉,其中的一些错误让每个人都感到困惑。
感谢所有伟大而快速的反馈!我现在已经理解我犯的错误了。谢谢!