Null-Coalescing Operator ( ?? )

Null-Coalescing Operator ( ?? )

If tempFileName is not null, fileName = tempFileName, else fileName = “Untitled“।

This can now be abbreviated as follows using the Null-Coalescing Operator:

string fileName = tempFileName ?? “Untitled”;
The logic is the same। If tempFileName is not null, fileName = tempFileName, else fileName = “Untitled“.

The Null-Coalescing Operator comes up a lot with nullable types, particular when converting from a nullable type to its value type:

int? count = null;

int amount = count ?? default(int);

Since count is null, amount will now be the default value of an integer type ( zero )।

These Conditional and Null-Coalescing Operators aren’t the most self-describing operators :) ,
but I do love programming in C#!

Leave a Reply


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.