【发布时间】:2012-02-07 01:19:48
【问题描述】:
这是一个错误还是我解释 '??' - 运算符错误?查看下面的 get 属性和 cmets。
我正在使用 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;
}
}
编辑:对于刚提出这个问题时看到这个问题的每个人表示抱歉,其中的一些错误让每个人都感到困惑。
感谢所有出色而快速的反馈!我现在已经理解我犯的错误了。 谢谢!
【问题讨论】:
-
我在您的代码示例中看不到
??的使用。
标签: c# .net-3.5 null-coalescing-operator