Use of question marks in C #

Source: Internet
Author: User

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 <T> (generic structure), that means when you use T? When the compiler compiles T? Compiled into the form of System. Nullable <T>.

Example: int ?, After compilation, it is in the form of System. Nullable <int>.

2. Ternary (operator) expression (? :):

Example: x? Y: z indicates that if expression x is true, return y. if expression x is false, return z, which is a simple form of if {} else {} omitted.

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. <Xmlnamespace prefix = "o" ns = "urn: schemas-microsoft-com: office"/>

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.