c#2.0 implementation of the nullable data type, although said to be just a small cookie, but have to say is C # vowed to continue its humanistic characteristics, we finally do not use object to store simple data to pass the ==null test. On the surface, this function may not be very innovative, but I do not know if you are like me in the memory of the same embedded in the same int a=null; not a compile-time complaint?
About nullable detailed introduction can refer to c#2.0 new and many blog articles, this is not what I want to say the main content. Only 2.0 in order to convert between nullable type and non-nullable data, a new operator "??" is provided. More interesting. The function of this operator is simple, as follows:
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 "??" , what can you think of in the first time? The first time I thought of the ternary operation? :!
Write a certain ternary expression in your code, which can often bring simplicity and compactness to our code. But anything will be in the ointment, this classic ternary operation must have two branches (well, if a branch is not ternary), so I sometimes have to write down some ugly code to not use the IF statement:
1.
string param = request.params["param"];
if (param = null)
{
param = defaultvalue;
} or
string param = request.params["param"] = = null? defaultvalue:request.params["param"]; & nbsp; I was more disgusted with the same code like request.params["key"], viewstate["key", and hasttable["key", written more than once, because as a key literal String cannot be checked by the compiler, and it is very maddening to have a spelling error.
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 after all, but it doesn't seem to be the ultimate seamless ...
In c#2.0, the use of "??" Operators, this kind of code will become very sexy:1. string params = reqeust.params["param"]?? DefaultValue;
2. public string GetValue {get {return this.value?? string. Empty; } }