Is this a bug or am I interpreting the '??'-operator wrong? Check out the get property below and the comments.
I'm using 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;
}
}
EDIT: Sorry to everybody who looked at the question when it was freshly asked, there where some errors in it that got to confuse everyone.
Thanks for all the great and fast feedback! I've now understood the error I made. Thanks!
This line:
Is equivalent to:
Not to:
The version with
??
, which you added later, is so unreadable that I won't analyze it. Let's get?
right first.