Hemant Kumar
Hemant Kumar

Reputation: 4611

Could any one explain on a Nullable types example?

Can someone explain this one too?

Getting a default value with Nullable Types:

int? n1=null; int n2=3;

(n1 ?? 10) will return the value 10.

int product= (n1 ?? 10) * n2; Now product will hold 30 since (n1??10) will return 10.

now,what does the statement " (n1 ?? 10) " means and why does it return the value '10'

Upvotes: 0

Views: 82

Answers (2)

Casey Wilkins
Casey Wilkins

Reputation: 2595

I don't usually program in C#, but ?? is the null-coalescing operator as described in MSDN's "?? Operator (C# Reference)".

n1 ?? 10

Basically says "If n1 is null, then change it to the default value of 10."

Upvotes: 1

Anton Semenov
Anton Semenov

Reputation: 6347

From MSDN:

The ?? operator is called the null-coalescing operator and is used to define a default value for a nullable value types as well as reference types. It returns the left-hand operand if it is not null; otherwise it returns the right operand.

I think any extra comment is not required

Upvotes: 3

Related Questions