concept comparison in category c#

appears as: comparisons, comparison, comparisons, comparison
C# in Depth, Fourth Edition

This is an excerpt from Manning's book C# in Depth, Fourth Edition.

In C# 1, the expression null always referred to a null reference. In C# 2, that meaning is expanded to a null value: either a null reference or a value of a nullable value type where HasValue is false. This can be used for assignments, method arguments, comparisons—any manner of places. It’s important to understand that when it’s used for a nullable value type, it really does represent the value of that type where HasValue is false rather than being a null reference; if you try to work null references into your mental model of nullable value types, it’ll get confusing quickly. The following two lines are equivalent:

int? x = new int?();

int? x = null;

I typically prefer to use the null literal over explicitly calling the parameterless constructor (I’d write the second of the preceding lines rather than the first), but when it comes to comparisons, I’m ambivalent about the two options. For example, these two lines are equivalent:

C# 7 introduces support for tuples to make data composition simple along with deconstruction from a composite type into its individual components. If you’re now thinking to yourself that C# already has tuples in the form of the System.Tuple types, you’re right to an extent; those types already exist in the framework but don’t have any language support. To add to the confusion, C# 7 doesn’t use those tuple types for its language-supported tuples. It uses a new set of System.ValueTuple types, which you’ll explore in section 11.4. There’s a comparison with System.Tuple in section 11.5.1.

Equality tests are performed element-wise using the default equality comparer for each element type. Similarly, element hash codes are computed using the default equality comparers, and then those hash codes are combined in an implementation-specific way to provide an overall hash code for the tuple. Ordering comparisons between tuples are performed element-wise too, with earlier elements being deemed more important in the comparisons than later ones, so (1, 5) compares as less than (3, 2), for example.