.net - What does '??' mean in C#? -


possible duplicate:
what 2 question marks mean in c#?

i'm trying understand statment does: "??" mean? som type if if-statment?

string cookiekey = "searchdisplaytype" + key ?? ""; 

it's null coalescing operator. means if first part has value value returned, otherwise returns second part.

e.g.:

object foo = null; object rar = "hello";  object = foo ?? rar; == "hello"; // true 

or actual code:

ienumerable<customer> customers = getcustomers(); ilist<customer> customerlist = customers ilist<customer> ??      customers.tolist(); 

what example doing casting customers ilist<customer>. if cast results in null, it'll call linq tolist method on customer ienumerable.

the comparable if statement this:

ienumerable<customer> customers = getcustomers(); ilist<customer> customerslist = customers ilist<customer>; if (customerslist == null) {      customerslist = customers.tolist(); } 

which lot of code compared doing within single line using null-coalescing operator.


Comments

Popular posts from this blog

ruby - When to use an ORM (Sequel, Datamapper, AR, etc.) vs. pure SQL for querying -

php - PHPDoc: @return void necessary? -

c++ - Convert big endian to little endian when reading from a binary file -