Question mark in C #

Source: Internet
Author: User

The question mark in C # is an important symbol in C #, which is often ignored by some people (?).

Here we will sort out several situations in C:
1. Can be empty type modifier "T? ": The basic type of the null type can be any non-null type or any type parameter with struct constraints, but cannot be a null type or reference type.
Example: int? Represents an integer that can be empty, and an int ?? The type is invalid. The null type can indicate all values of the basic type and an additional null value. Syntax T? Is the abbreviation form of System. Nullable. The empty type has a Boolean read-only attribute of HasValue. If the attribute of a null type instance is true, it indicates that the instance is a non-empty instance and contains a known Value; if HasValue is false, access to the Value attribute will result in System. invalidOperationException. Can be empty type T? A public constructor with a single parameter of the type T, such as new int? (123) will get an int with a value of 123? Type instance. From T? To any interface implemented by T, there is a packing conversion, and from any interface implemented by T to T? . But in any case, the null type does not meet the interface constraints, even if the basic type implements this specific interface.

2. Conditional operator "x? Y: z ":" x? Y: z "means that most programmers should be familiar with it. If expression x is true, calculate y. If expression x is false, then calculate z. Saves the simple form of if {} else. 3. Empty merge operator "?? ": This operator is a newly added mark in the C # lexical syntax after the occurrence of generics. A": "(namespace alias qualifier) is also displayed ). The format is "?? The null merge expression of B requires that a BE OF THE null or reference type. If expression a is not null, the expression "?? B "returns a; otherwise, returns B. The null merge operator is the right merge operator, that is, the Union from right to left during the operation. For example, "?? B ?? C "in the form of" ?? (Bb ?? Cc) "computing.

Someone once said, "Details determine the outcome". Our programmers should better grasp the details of programming languages to better ensure the quality of software.

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.