no suspense, straight to the point:
The role of two question marks in C # is to judge?? Is the object on the left null, and if it is not NULL then use?? The object on the left, if NULL, is used?? The object on the right.
For example: a = b?? C, if B is null, A = C and if B is not null, then A = B.
The following code illustrates both scenarios:
?
12345678910111213141516171819 |
static void Main(string[] args)
{
NewFuck();
Shit();
Console.ReadKey();
}
private static void NewFuck()
{
string fuck =
"Fuck"
;
Console.WriteLine(fuck ??
"fuck not found."
);
}
private static void Shit()
{
object shit =
null
;
string b = (string)shit ??
"shit not found."
;
Console.WriteLine(b);
}
|
It is obvious that this operator, like the trinocular expression, can be used to install B in front of a sister and see a visual example:
?
12345678910111213141516171819202122232425262728293031 |
//文艺Fuck
private static void NewFuck()
{
string fuck =
"Fuck"
;
Console.WriteLine(fuck ??
"fuck not found."
);
} //普通Fuck
private static void Fuck()
{
string fuck =
"Fuck"
;
string s = fuck !=
null ? fuck :
"fuck not found."
;
Console.WriteLine(s);
}
//2BFuck
private static void SBFuck()
{
string fuck;
fuck =
"Fuck"
;
string s;
if (fuck !=
null
)
{
s = fuck;
}
else
{
s =
"fuck not found."
;
}
Console.WriteLine(s);
}
|
These three methods are the same on the result, and will output fuck on the screen. Of course, write an example is only the demo level, we in the actual programming time with?? Operators are often more useful and can save a lot of trouble. For example, when dealing with page querystring:
?
12345678910111213 |
// 原先要这样处理参数:
string tmd = String.Empty;
if (Request[
"Select"
] !=
null
)
{
tmd = Request[
"Select"
];
}
else
{
tmd =
"All"
;
}
// 现在重构为:
string tmd1 = Request[
"Select"
] ??
"All"
;
|
What do you think? Have you ever felt a dick explode? The session can also be handled in this way. Incidentally, the processing parameters in the actual project are more complex than this. Operations such as type conversions are often included. The type conversion proposal uses the as operator, which is not strong.
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace ConsoleApplication1
{
Class Program
{
static void Main (string[] args)
{
Int? A
A = null;//at this time output 3
A = 6;//at this time output 6
int b = a?? 3;
Console.Write (b);
Console.readkey ();
}
}
}
The variable definition contains a question mark, meaning that the data type is of type nullable.
Variable definition contains two question marks, binocular operator, meaning to take the assigned value?? To the left, if NULL on the left, take the assigned value?? On the right.
The role of a question mark and two question mark (a?? b) in C #