這是一個錯誤還是我解釋'??' - 操作符錯誤?查看下面的獲取屬性和評論。在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