这是一个错误还是我解释'??' - 操作符错误?查看下面的获取属性和评论。在C#3.5中'??'运算符的奇怪行为
我使用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;
}
}
编辑:对不起大家谁看了看问题,当它刚问,那里有它的一些错误,进行了迷惑大家。
感谢所有伟大和快速的反馈!我现在明白了我所犯的错误。 谢谢!
我在您的代码示例中看不到'??'的用法。 – RedFilter 2012-01-10 16:16:04