Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming in C# Comparison (Sorting)

Similar presentations


Presentation on theme: "Programming in C# Comparison (Sorting)"— Presentation transcript:

1 Programming in C# Comparison (Sorting)
CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis

2 Comparison Comparing two instances of a type has many of the same properties and pitfalls as testing for equality. Equality is generally more fussy. Only two protocols for a type providing its own comparison: The IComparable interface The > and < operators

3 IComparable The IComparable<T> and IComparable interfaces allow for sorting or ordering of a collection. 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”) => 1 32.CompareTo(8*4) => 0

4 IComparable Classes implementing IComparable are
Values types like Int32, Double, DateTime, … The class Enum as base class of all enumeration types The class String Defines a type to be is-a IComparable.

5 The > and < operators
Value types that have a clear context independent concept of less than and greater than should implement the < and > operators. These are compiled statically into the code, making value types more efficient.

6 Programming in C# Comparison (Sorting)
CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis


Download ppt "Programming in C# Comparison (Sorting)"

Similar presentations


Ads by Google