Although C #2.0 implements the nullable data type, although it is just a small cookie, it has to be said that C # is persistently continuing its humanized features, finally, we don't need to use an object to store simple data to pass the = NULL test. On the surface, this function may not have much innovation significance, but I wonder if you have any complaints like int A = NULL in your memory like me?
For details about nullable, refer to the new features and many blogs in C #2.0.ArticleThis is not what I want to talk about. Only 2.0 provides a new operator "?? "Interesting. This operator has the following functions:
Int ? A = 1 ;
Int ? B = Null ;
Int C = A; // Compile error :(
Int C = A ?? 100 ; // Right
Int D = A + B; // Compile error yet
Int D = A + B ?? - 1 ; // Right
See this "?? "Use, what can you think of in the first time? I first thought of it.Three-element operationOperation? :!
InCodeTo write a certain Trielement expression in the code, which is often concise and compact. However, nothing is perfect. This classic ternary operation must have two branches (well, if one branch is not a ternary operation), so sometimes I have to avoid using the if statement, write some ugly code:
1.
String Param = Request. Params [ " Param " ];
If (Param = Null )
{
Param = defaultvalue;
}
Or
String Param = Request. Params [ " Param " ] = Null ? Defaultvalue: request. Params [ " Param " ];
I am disgusted with the request. Params ["key"]The same Code such as viewstate ["key"] And hasttable ["key"] is written more than once, because the literal string as the key cannot be checked by the compiler, spelling mistakes are very frustrating.
2.
Public String Getvalue
{
Get
{
If ( This . Value = Null )
{
Return String. Empty;
}
Else
{
Return This. Value;
}
}
}
Or
Public String Getvalue
{
Get
{
Return This. Value= Null ? String. Empty:This. Value;
}
}
Use? : It looks good later, but it does not seem to be our hope...
In C #2.0 "?? "Operator, this type of code will become very sexy:
1. String Params = Reqeust. Params [ " Param " ] ?? Defaultv Alue;
2. Public String Getvalue {Get {Return This. Value?? String. Empty ;}}
3. bool isinternal = This. session ["isinternal"] As bool? ?? False;