What does the first line in this MVC project mean?

Possible Duplicate:
What do two question marks together mean in C#?

I am looking at an MVC project and the first line in a cshtml page is:

@if (Model.DatabaseIssue ?? false) {

}

What does this mean? What is the double ?? and why is it used?


that line uses Model.DatabaseIssue when Model.DatabaseIssue is not null , otherwise uses false .

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.

?? Operator (C# Reference)


?? is a C# operator which returns the left side if its not null and otherwise the right side.

So in your case it will return the value of Model.DatabaseIssue if its not null. If its null it will return false .

Model.DatabaseIssue seems to have the type bool? (nullable bool).


If Model.DatabaseIssue is not null, use it, otherwise, use false.

It's using the coalesce operator in C#. http://msdn.microsoft.com/en-us/library/ms173224.aspx

链接地址: http://www.djcxy.com/p/53844.html

上一篇: 在C#中,它是什么意思'??'?

下一篇: 这个MVC项目的第一行是什么意思?