Sunday, 15 January 2012

?? null coalescing operator

.NET 3.5 questions:

Q1: What is the output of the following code?

int a = 20;
int? b = 30;
int? c = null;
Console.WriteLine(a + c ?? b);

A1:
result is 30

int?  indicates b and c are nullable types 
The result of 20+null is null

?? operator checks whether the value of a+c is null, it returns a+c if it isn't null, returns b if it is null.
?? operator works for both reference and value types.


Q2: what is the output of the following code?
 
var s= "Hello";
s = 5;
s = 5++;
Console.WriteLine(s);

Cannot compile, error is "cannot implicitly convert type 'int' to 'string'

No comments:

Post a Comment