Search This Blog

Monday, 25 July 2011

Ternery ("Shortcuts"for the rest of us who speak English!") Expression in C#

Probably the least well (clear) documented aspects of C# , so i decided to list a few basics


var f;

if (a == b)
f = DoThis()
else
f = DoThat();



Ternery ->

f = a == b ? doThis() : DoThat()



var f;

if (a != null)
f = DoThis()
else
f = null;


Ternery ->

f = a && DoThis() ;




var f;

if (f == null)
f = DoThis()


Ternery ->

f ?? DoThis() ;

No comments:

Post a Comment

Popular Posts