开发者

Could any one explain on a Nullable types example?

开发者 https://www.devze.com 2023-03-01 14:25 出处:网络
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.

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 doe开发者_JAVA百科s it return the value '10'


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号