Learning null-coalescing operator

I am pretty sure that most of the guys has never knew this operator and its also very rarely used. Since C# provides this and this is very useful and handy at certain times.

The short from of null-coalescing operator is ??.

It actually takes two operands.  So it returns the left hand operand if it is not null else returns right operand. So this is very useful while initializing any variable. let’s see a example

string name = null;
string otherName = "Brij";
string userName = name ?? otherName;
Console.WriteLine(userName);

As you can see, here is name is null then userName is assigned to otherName. and it will print Brij.

Continue reading

Advertisements