Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     




Null coalescing Operator (?? operator)

Null coalescing Operator (?? operator) is used with nullable types and reference types. It is used to compare the values if there is an null value between two values. both values should be of same type e.g. integer. Null coalescing Operator (?? operator) compare the both values and return the first value if first value is not null. If first value is null then it will go for second value.

Example code is given below:

 int? i = null;
int? j = 10;

int k;
k = i ?? 20;

Response.Write("Result for k = i ?? 20 is " + k + "");

k = j ?? 30;

Response.Write("Result for k = j ?? 30 is " + k + "");

In this code we are taking the variable i which is null and j which having value is 10. Another variable is k which is using to get assigned the value.

k = i ?? 20;

in this line compiler will assign the value 20 to k if i is null.

k = j ?? 30;

in this vale of k is 10 because j is not empty. compiler will check the first value. if that is null the it will go to second value. Both values which we are comparing should be of same type e.g. integer

Share this article   |    Print    |    Article read by 2902 times
Author:
Rohit kakria
I am software developer
Related Articles: No related article
Related Interview Questions: No related interview question