CSE459_CSharp_07_Comparison

CSE459_CSharp_07_Comparison - => 1...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Programming in C# Comparison (Sorting) CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Comparison l Comparing two instances of a type has many of the same properties and pitfalls as testing for equality. l Equality is generally more fussy. l Only two protocols for a type providing its own comparison: l The IComparable interface l The > and < operators
Background image of page 2
IComparable l The IComparable<T> and IComparable interfaces allow for sorting or ordering of a collection. l They are used in the Array.Sort method. public interface IComparable <T>{ int CompareTo (T other); // -1 if this < other, 0 if this == other, 1 if this > other } 5.CompareTo(7) => -1 “World”.CompareTo(“Hello”)
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: =&gt; 1 32.CompareTo(8*4) =&gt; 0 IComparable l Classes implementing IComparable are l Values types like Int32, Double, DateTime, l The class Enum as base class of all enumeration types l The class String l Defines a type to be is-a IComparable. The &gt; and &lt; operators l Value types that have a clear context independent concept of less than and greater than should implement the &lt; and &gt; operators. l These are compiled statically into the code, making value types more efficient. Programming in C# Comparison (Sorting) CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis...
View Full Document

This note was uploaded on 02/27/2012 for the course CSE 459.24 taught by Professor Crawfis during the Winter '11 term at Ohio State.

Page1 / 6

CSE459_CSharp_07_Comparison - =&amp;amp;gt; 1...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online