C#2.0 finally got it?: one-branch version of convenient judgment

Source: Internet
Author: User
Tags empty key

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; } }



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.