In C # "? "There are three ways to use

Source: Internet
Author: User

1. Nullable type modifier (? ): A reference type can use a null reference to represent a nonexistent value, and a value type usually cannot be represented as empty, for example: string str=null; int i=null; The compiler will make an error. For the value type to be nullable, the nullable type appears, and the nullable type uses the nullable type modifier? To indicate that the representation is T?. Example: Int? Represents a nullable shape, datetime, which is represented as a nullable time. T? is actually an abbreviated form of system.nullable<t> (generic structure), which means when you use T? At compile time, the compiler will put T? Compiled into the form of system.nullable<t>, for example: int, is the form of system.nullable<int> after compilation.

2, ternary (operator) expressions (?) :): For example int a=b>0?4:5 if B is greater than 0 returns a=4 otherwise a=5.

3. Empty merge operator (??). ): Defines the default values for nullable types 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: A?? b, when a is null, returns a b,a when it is not empty to return a itself. An empty merge operator is a right-associative operator, which is a combination of right-to-left operations. For example, "a??" B?? C "In the form of" a?? (b?? c) "calculation.


This article is from the "Technical Experience sharing" blog, please be sure to keep this source http://yataigp.blog.51cto.com/4779773/1983948

In C # "? "There are three ways to use

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.