The role of a question mark and two question mark (a?? b) in C #

Source: Internet
Author: User

no suspense, straight to the point:

The role of two question marks in C # is to judge?? Is the object on the left null, and if it is not NULL then use?? The object on the left, if NULL, is used?? The object on the right.

For example: a = b?? C, if B is null, A = C and if B is not null, then A = B.

The following code illustrates both scenarios:

?
12345678910111213141516171819 static void Main(string[] args){    NewFuck();    Shit();    Console.ReadKey();}private static void NewFuck(){    string fuck = "Fuck";    Console.WriteLine(fuck ?? "fuck not found.");}private static void Shit(){    object shit = null;    string b = (string)shit ?? "shit not found.";    Console.WriteLine(b);}

It is obvious that this operator, like the trinocular expression, can be used to install B in front of a sister and see a visual example:

?
12345678910111213141516171819202122232425262728293031 //文艺Fuckprivate static void NewFuck(){    string fuck = "Fuck";    Console.WriteLine(fuck ?? "fuck not found.");} //普通Fuckprivate static void Fuck(){    string fuck = "Fuck";    string s = fuck != null ? fuck : "fuck not found.";    Console.WriteLine(s);}     //2BFuckprivate static void SBFuck(){    string fuck;    fuck = "Fuck";    string s;    if (fuck != null)    {        s = fuck;    }    else    {        s = "fuck not found.";    }    Console.WriteLine(s);}

These three methods are the same on the result, and will output fuck on the screen. Of course, write an example is only the demo level, we in the actual programming time with?? Operators are often more useful and can save a lot of trouble. For example, when dealing with page querystring:

?
12345678910111213 // 原先要这样处理参数:string tmd = String.Empty;if (Request["Select"] != null){    tmd = Request["Select"];}else{    tmd = "All";}// 现在重构为:string tmd1 = Request["Select"] ?? "All";

What do you think? Have you ever felt a dick explode? The session can also be handled in this way. Incidentally, the processing parameters in the actual project are more complex than this. Operations such as type conversions are often included. The type conversion proposal uses the as operator, which is not strong.

Using System;

Using System.Collections.Generic;

Using System.Linq;

Using System.Text;

Namespace ConsoleApplication1

{

Class Program

{

static void Main (string[] args)

{

Int? A

A = null;//at this time output 3

A = 6;//at this time output 6

int b = a?? 3;

Console.Write (b);

Console.readkey ();

}

}

}

The variable definition contains a question mark, meaning that the data type is of type nullable.

Variable definition contains two question marks, binocular operator, meaning to take the assigned value?? To the left, if NULL on the left, take the assigned value?? On the right.

The role of a question mark and two question mark (a?? b) in C #

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.