C # operator?、??、?: Usage and description of various question marks _c# tutorial

Source: Internet
Author: User
Tags modifier

1, nullable type modifier (?) : A reference type can use a null reference to represent a nonexistent value, and a value type is usually not represented as empty, for example: string str=null; is correct. int i=null; The compiler will make an error. For a value type to be nullable, a nullable type appears, and a nullable type uses a nullable type modifier to indicate that the representation is T? For example: int? is a nullable shaping, DateTime is expressed as a nullable time. T? is actually an abbreviated form of system.nullable<t> (generic structure), which means that when you use T, the compiler compiles the t into a system.nullable<t> form, for example: int? After compiling is the form of system.nullable<int>.

Copy Code code as follows:

int A; A<>null
int? b; B=null
int? c = b+1; C=null;

2, empty merge operator (??) : Used to define the default values for nullable types and reference types. If the left-hand operand of this operator is not NULL, the operator returns the left operand, otherwise the right-hand operand is returned. Example: A?? b, when a is null, returns the b,a when it is not empty. The null merge operator is the right binding operator, which is a combination of right-to-left and right-to-left operations. such as, "a??" B?? C "In the form of" a?? (b??) c) "calculation.

Copy Code code as follows:

Int?a=null; int b; (declarations A and B)
B=a?? 2; b=2;
A=6;b=a?? 8;//b=6;

3, ternary (operator) expressions (?:) : If this you do not understand the words (I do not believe), then you should not understand it, no longer repeat.

Copy Code code as follows:

int a=1>0?1:0//a=1;

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.