0

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'

Hemant Kumar
  • 4,593
  • 9
  • 56
  • 95

2 Answers2

3

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

Community
  • 1
  • 1
Anton Semenov
  • 6,227
  • 5
  • 41
  • 69
1

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."

Jeromy French
  • 11,812
  • 19
  • 76
  • 129
Casey Wilkins
  • 2,555
  • 2
  • 23
  • 31