Read the scottgu's blog today and see this article: The C #?? Null coalescing operator (and using it with LINQ) .
I think it's really fun. If you think it might be useful later, I plan to take the big bull's Article I moved to my home. e is not good. I will write it according to my own understanding. I will leave the original article for other cool people to work hard ~
Pay attention to what msdn will find? : Operator and ?? Operators do almost the same thing, but the former applies more widely (self-perception) and copies the msdn dogma to list the differences:
1 ).? : Operator (C # reference)
Conditional operators (? :) Return one of the two values based on the value of the Boolean expression. The format of conditional operators is as follows:
Condition? First_expression: second_expression;
2 ).?? Operator (C # reference)
If ?? The left operand is not null. This operator returns the left operand; otherwise, the right operand is returned.
In this way, we can see that they are simplifying the if judgment expression, ?? The operator focuses on determining whether the object is null and then giving the result. Here scottgu gave a question ?? For the example of operators, see
Because the message is not null, The result returned by the result is "helllo world". Therefore, if you want to return the result "It's was null", it should be:
So ?? Is the operator only applicable to reference types? Can it be used in the value type? Scottgu then provides an example of using the value type. For details, see:
As shown in figure 1, because number is not null, The result returned by result is the value of number instead of 0. To return 0, perform the following operations:
So what is this ?? Operator, now enter
Topic Now, in
Use question mark (?) in LINQ ?? Operator .
Scottgu is used here to prepare to use xlinq (LINQ to XML). Therefore, the data source is an XML file: contacts. xml. The XML file structure is as follows:
Next, write C # Code Read the content of the XML file and bind the read data to the gridview. The displayed fields are: "name", "title", "email", "yearsatcompany" in sequence ".
The code for getting data is as follows:
The interface code is not too long here. Drag a gridview and place it on the page. Run this example. Program , As shown below:
When I write it here, I find that there is nothing in the code ?? Operator trace, don't worry, continue down, foreigners write Dongdong, always like from simple to deep, 1.1 point squeeze toothpaste:
Modify the original XML file and add a new <contact> section, in which the <yearsatcompany> sub-bytes are removed. The structure is as follows:
The original code must be modified. Because the "yearsatcompany" value is a null integer during the next query, we must use (Int ?) Replace the original (INT) type for conversion
"Yearsatcompany" value, the Code is as follows:
Use (Int ?) Is replacement (INT) finished? I think it is impossible. The page after running proves this point:
Er, there are numbers, but the third "yearsatcompany" value is a big whiteboard, ugly! If it is null, it should be replaced with 0.
Haha, now we can make it ?? The operator is out of the box, so it is much easier to do with it. The improved code is as follows:
After such a hard job, the page finally shows what we want:
This is used in xlinq ?? Operator. Similarly, in other types of LINQ, we can also introduce ?? Operator to simplify our code.