.net 3.5 - Strange behavior of the '??'-operator in C# 3.5 -
.net 3.5 - Strange behavior of the '??'-operator in C# 3.5 -
is bug or interpreting '??'-operator wrong? check out property below , comments.
i'm using c# .net 3.5
private list<mytype> _mytypelist; private list<mytype> mytypelist { { //the 2 code lines below should each behave 3 under, don't? //the ones uncommented working, commented result in list returning empty (newly created suppose). //return _mytypelist ?? new list<mytype>(); //return _mytypelist == null ? new list<mytype>() : _mytypelist; if (_mytypelist == null) _mytypelist = new list<mytype>(); homecoming _mytypelist; } }
edit: sorry looked @ question when freshly asked, there errors in got confuse everyone.
thanks great , fast feedback! i've understood error made. thanks!
this line:
return _mytypelist == null ? new list<mytype>() : _mytypelist;
is equivalent to:
if (_mytypelist == null) homecoming new list<mytype>(); homecoming _mytypelist;
not to:
if (_mytypelist == null) _mytypelist = new list<mytype>(); homecoming _mytypelist;
the version ??
, added later, unreadable won't analyze it. let's ?
right first.
c# .net-3.5 null-coalescing-operator
Comments
Post a Comment