C # Two question marks (?? B) Yes? Meow?

Source: Internet
Author: User

In C #, the two question marks are used to determine ?? Is the object on the left null? If not null ?? The object on the left. If it is null, use ?? Object on the right.

For example, a = B ?? C. If B is null, A = C. If B is not null, A = B.

The following sectionCodeThese two cases are demonstrated:

01 Static Void Main (String[] ARGs)
02 {
03 Newfuck ();
04 Shit ();
05 Console. readkey ();
06 }
07  
08 Private Static Void Newfuck ()
09 {
10 String Fuck ="Fuck";
11 Console. writeline (fuck ??"Fuck not found .");
12 }
13  
14 Private Static Void Shit ()
15 {
16 Object Shit =Null;
17 String B = (String) Shit ??"Shit not found .";
18 Console. writeline (B );
19 }

The execution result is as follows:

Obviously, this operator similar to the three-object expression can be used to install B in front of the sister. Let's look at an intuitive example:

01 // Literature and art fuck
02 Private Static Void Newfuck ()
03 {
04 String Fuck ="Fuck";
05 Console. writeline (fuck ??"Fuck not found .");
06 }
07  
08 // General fuck
09 Private Static Void Fuck ()
10 {
11 String Fuck ="Fuck";
12 String S = fuck! =Null ? Fuck:"Fuck not found .";
13 Console. writeline (s );
14 }
15  
16 // 2 bfuck
17 Private Static Void Sbfuck ()
18 {
19 String Fuck;
20 Fuck ="Fuck";
21 String S;
22 If (Fuck! =Null)
23 {
24 S = Fuck;
25 }
26 Else
27 {
28 S ="Fuck not found .";
29 }
30 Console. writeline (s );
31 }

The three methods are the same in the result and will output the fuck on the screen. Of course, writing an example is only demo-level. We use it in actual programming ?? Operators are often more useful and can save a lot of trouble. For example, when processing the querystring page:

01 // The processing parameters are as follows:
02 String TMD = string. empty;
03 If (Request ["Select"]! =Null)
04 {
05 TMD = request ["Select"];
06 }
07 Else
08 {
09 TMD ="All";
10 }
11  
12 // Now refactor:
13 String Tmd1 = request ["Select"] ???"All";

How is it? Do you think it is amazing. This can also be done for sessions or something. By the way, the processing parameters in the actual project are more complex than this one. It usually includes operations such as type conversion. We recommend that you use the as operator for type conversion.

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.