C #2.0 is there? : Easy to judge single branch Edition

Source: Internet
Author: User
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;

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.