C # medium ?? ,? ,? :,?. ,? [],

Source: Internet
Author: User

C # medium ?? ,? ,? :,?. ,? [],

1. Can be empty type modifier (?)
The reference type can use null reference to indicate a nonexistent value, while the value type cannot be null.
For example, string str = null; is correct, int I = null; the compiler reports an error.
To make the value type empty, you can use the void type, that is, the void type modifier "? "T? "
Example: int? It indicates an integer that can be null, DateTime? It indicates a time that can be empty.
T? It is actually the abbreviation form of System. Nullable (generic structure,
That means when you use T? When the compiler compiles T? Compiled in the form of System. Nullable.
Example: int ?, After compilation, it is in the form of System. Nullable.

2. Ternary (operator) expression (? :)
Example: x? Y: z indicates that if expression x is true, return y;
If x is false, return z, which is a simple form of omitting if {} else.

3. null merge operator (??)
Defines the default values of the null and reference types.
If the left operand of this operator is not null, this operator returns the left operand; otherwise, the right operand is returned.
Example: ?? B returns B if a is null, and a itself if a is not null.
The null merge operator is the right merge operator, that is, the Union from right to left during the operation.
For example, "?? B ?? C ?? (B ?? C) "computing.

4. NULL Check operator (?.)

For example, if we want to obtain the X coordinate of the first vertex of a Point sequence, we will write it like this:

Int firstX = points. First (). X;

However, laruence will tell you that there is no NULL check. The correct version is as follows:

   int? firstX = null;    
  if (points != null)     
{  
 var first = points.FirstOrDefault();   
     if (first != null)        
     firstX = first.X;   
 }

It is correct, and the code extraction is much harder to read. In C #6.0, ?. The preceding code can be changed to the following format:

Int? FirstX = points ?. FirstOrDefault ()?. X;

In this example, we can also see itsBasic usage: if the object is NULL, NULL is directly returned without the subsequent operations for obtaining members.

Note that because "?. "The operator can return NULL. When the returned member type is struct ,"?. The Return Value Type of the "." and "." operators is different.

Point p = new Point (3, 2 );

Console. WriteLine (p. X. GetType () = typeof (int); // true

Console. WriteLine (p ?. X. GetType () = typeof (int ?)); // True

5 "? [] "OPERATOR:

Int? First = customers? [0]. Orders. Count ();

(This is not clear. I feel that Microsoft's syntax is too powerful .)

Essence: All use syntactic sugar. During the compilation phase, the compiler first translates the special symbols into the original state and then compiles them.

Source: http://blog.sina.com.cn/s/blog_72463843010195ru.html

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.