13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

82 CHAPTER 3 Parameterized typ<strong>in</strong>g with generics}...Console.WriteL<strong>in</strong>e(CompareToDefault("x"));Console.WriteL<strong>in</strong>e(CompareToDefault(10));Console.WriteL<strong>in</strong>e(CompareToDefault(0));Console.WriteL<strong>in</strong>e(CompareToDefault(-10));Console.WriteL<strong>in</strong>e(CompareToDefault(DateTime.M<strong>in</strong>Value));List<strong>in</strong>g 3.4 shows a generic method be<strong>in</strong>g used with three different types: str<strong>in</strong>g,<strong>in</strong>t, and DateTime. The CompareToDefault method dictates that it can only beused with types implement<strong>in</strong>g the IComparable <strong>in</strong>terface, which allows us to callCompareTo(T) on the value passed <strong>in</strong>. The other value we use for the comparison is thedefault value for the type. As str<strong>in</strong>g is a reference type, the default value is null—andthe documentation for CompareTo states that for reference types, everyth<strong>in</strong>g should begreater than null so the first result is 1. The next three l<strong>in</strong>es show comparisons with thedefault value of <strong>in</strong>t, demonstrat<strong>in</strong>g that the default value is 0. The output of the last l<strong>in</strong>eis 0, show<strong>in</strong>g that DateTime.M<strong>in</strong>Value is the default value for DateTime.Of course, the method <strong>in</strong> list<strong>in</strong>g 3.4 will fail if you pass it null as the argument—the l<strong>in</strong>e call<strong>in</strong>g CompareTo will throw NullReferenceException <strong>in</strong> the normal way.Don’t worry about it for the moment—there’s an alternative us<strong>in</strong>g IComparer, aswe’ll see soon.DIRECT COMPARISONSAlthough list<strong>in</strong>g 3.4 showed how a comparison is possible, we don’t always want to constra<strong>in</strong>our types to implement IComparable or its sister <strong>in</strong>terface, IEquatable,which provides a strongly typed Equals(T) method to complement the Equals(object)method that all types have. Without the extra <strong>in</strong>formation these <strong>in</strong>terfaces give us accessto, there is little we can do <strong>in</strong> terms of comparisons, other than call<strong>in</strong>g Equals(object),which will result <strong>in</strong> box<strong>in</strong>g the value we want to compare with when it’s a value type.(In fact, there are a couple of types to help us <strong>in</strong> some situations—we’ll come to them<strong>in</strong> a m<strong>in</strong>ute.)When a type parameter is unconstra<strong>in</strong>ed (<strong>in</strong> other words, no constra<strong>in</strong>ts are appliedto it), you can use == and != operators but only to compare a value of that type withnull. You can’t compare two values of T with each other. In the case where the typeargument provided for T is a value type (other than a nullable type), a comparison withnull will always decide they are unequal (so the comparison can be removed by the JITcompiler). When the type argument is a reference type, the normal reference comparisonwill be used. When the type argument is a nullable type, the comparison will do theobvious th<strong>in</strong>g, treat<strong>in</strong>g an <strong>in</strong>stance without a value as null. (Don’t worry if this last bitdoesn’t make sense yet—it will when you’ve read the next chapter. Some features aretoo <strong>in</strong>tertw<strong>in</strong>ed to allow me to describe either of them completely without referr<strong>in</strong>g tothe other, unfortunately.)When a type parameter is constra<strong>in</strong>ed to be a value type, == and != can’t be used withit at all. When it’s constra<strong>in</strong>ed to be a reference type, the k<strong>in</strong>d of comparison performeddepends on exactly what the type parameter is constra<strong>in</strong>ed to be. If it’s just a referencetype, simple reference comparisons are performed. If it’s further constra<strong>in</strong>ed toLicensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!