C # Wonderful functions-5. Nullable static class

Source: Internet
Author: User

Some time ago, I was too busy to write blogs. Today I want to come and have a look at a wonderful function.

First, assume that you have to deal with some existing code and use nullable (null type) variables. You must use this variable to compare with non-empty variables.

Use <,>, <=,> = to compare the nullable value.
See the following code:

1: int? X = null; 2: 3: if (x <100) 4: {5: Console. WriteLine ("Yes, {0} less than 100.", 6: x. HasValue? X. toString (): "null"); 7:} 8: else9: {0: Console. writeLine ("No, {0} is not less than 100. ", 1: x. hasValue? X. ToString (): "null"); 2 :}
Some people may think that either the result is "yes" or the NullReferenceException is thrown, right? Because a null integer is smaller than 100 in our understanding, but the running result is "no "!

 

The following example is even more confusing, because the result is "no ":

1: int? X = null; 2: 3: if (x <int. MaxValue) 4: {5: //... 6 :}
So, let's say that null is less than all valid integer types, right? If yes, what do you think of this Code:

1: int? X = null; 2: 3: //, etc. X is not less than MinValue? 4: if (x <int. MinValue) 5: {6: //... 7 :}
If we use the greater than symbol to judge:

1: int? X = null; 2: 3: // x is not greater than MinValue .... 4: if (x> int. MinValue) 5: {6: //... 7 :}
All the four comparison operators (<,>,<=, >=) are used to return no. This applies to other types defined by such operators: short, float, double, DateTime, and TimeSpan.


In the. net mechanism, null is not a large value or a small value, and no exception occurs during comparison.

 

Even more strange:

1: DateTime? X = null; 2: DateTime? Y = null; 3: 4: if (x <= y) 5: {6: Console. WriteLine ("do you think they are equal? "); 7:} 8: else 9: {10: Console. writeLine ("the result is not: <=, <,>,> = does not apply to null. "); 11 :}
Let's look at the following code:

1: int? X = null; 2: int? Y = 100; 3: 4: if (x <y) 5: {6: Console. writeLine ("X less than Y"); 7:} 8: else if (x> y) 9: {10: Console. writeLine ("X greater than Y"); 11:} 12: else 13: {14: // This Is the result 15: Console. writeLine ("X equals Y"); 16 :}..
Solution: nullable static class
Therefore, we can see that <, <=,>, and> = have some interesting unexpected behaviors.

However, what should we do if we regard null as a very low value?

For example, if we want to sort a list. The current stock price of the company is displayed in the list:

 

Codename description price
---------------------------------------
ABCZ Apples and Oranges Inc n/
XYZP Zippers and Buttons Inc 1.57
AZAZ Carrots and Turnips Inc 23.13

Comparison operators are not applicable to null, so we have to do some very complex logic, such:

1: if (x. hasValue) 2: {3: if (y. hasValue) 4: {5: if (x <y) 6: {7: Console. writeLine ("x <y"); 8:} 9: else if (x> y) 0: {1: Console. writeLine ("x> y"); 2:} 3: else 4: {5: Console. writeLine ("x = y"); 6:} 7:} 8: else 9: {0: Console. writeLine ("x> y because y is null, x is not"); 1:} 2:} 3: else if (y. hasValue) 4: {5: Console. writeLine ("x <y because x is null, y is not"); 6:} 7: else 8: {9: Console. writeLine ("x = y because both are null"); 0 :}
 

We may be able to simplify this function, but it is still quite messy! But there is a more clean way to do this,

If you read on MSDN how IComparer works:

When IComparable is used, Nothing can be compared with any type without generating exceptions. When sorting, Nothing is considered to be smaller than any other object.
Therefore, we can change our logic using the Nullable. Compare <T> () static method:

1: int? X = null; 2: int? Y = 100; 3: 4: if (Nullable. compare (x, y) <0) 5: {6: // 7: Console. writeLine ("x <y"); 8:} 9: else if (Nullable. compare (x, y)> 0) 0: {1: Console. writeLine ("x> y"); 2:} 3: else 4: {5: Console. writeLine ("x = y"); 6 :}
Summary
Therefore, when comparing two values, one of which may be null, consider using the System. Nullable. Compare <T> () method instead of the comparison operator. It treats null as smaller than any value, and uses it to avoid logical consistency problems, such as <false,> = true.

This article is from "Happy ASP. NET (Alex Song )"

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.