Embed Code

Generics, Lists, Interfaces PowerPoint PPT Presentation

.NET Generic Collections. Generics, Lists, Interfaces. .NET Collections and Generics. A look back. .NET Collection Classes. C# has been through several versions since its inception The first version had a set of Collection classes that represented various data structures

Copyright Complaint Adult Content Flag as Inappropriate

Download Presentation

Generics, Lists, Interfaces

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

List <T> allows duplicateelements – that is, the same value may appear in the List<T> more than once

The IEquatable<T> Interface

Implementing the IEquatable<T> interface assures that data of type T can be compared for equality

T t1, t2;

if ( t1.Equals(t2) ) …

If you implement IEquatable <T>, you should also override the base class implementations of Object.Equals(Object) and GetHashCode so that their behavior is consistent with that of the IEquatable<T>.Equals method

If you do override Object.Equals(Object) , your overridden implementation is also invoked in calls to the static Equals(System.Object,System.Object) method on your class

This ensures that all invocations of the Equals method return consistent results

Partial IEquatable<T> Example

IEquatable<T>.Equals

Compares 2 Persons

Override of Object.Equals

Compares Person to any object

Uses IEquatable<T> version

Override of Object.GetHashCode

Full Example

IEquatable <User> Implementation

Compare two Users

Compare User to any object

If Users are equal, their hash codes should be equal, too

The IComparable<T> Interface

Some methods of the List<T> class require that we be able tocompare 2 items of type T to determine their order

Examples include the following methods

Sort

BinarySearch

T must implement the IComparable<T>interface

T must implement a CompareTo<T> method to compare two items of type T

See the next slide for the rules that the CompareTo<T> method must follow

Rules for CompareTo<T>

For objects A, B, and C of type T these must be true:

A.CompareTo (A) is required to return zero(i.e., A == A)

If A.CompareTo(B) returns zero, then B.CompareTo(A) is required to return zero (i.e., if A==B, then B==A)

If A.CompareTo(B) returns zero and B.CompareTo (C) returns zero, then A.CompareTo (C) is required to return zero (if A==B and B==C, then A==C)

If A.CompareTo (B) returns a value other than zero, then B.CompareTo (A) is required to return a value of the opposite sign (A > B  B < A and A < B  B > A)

If A.CompareTo (B) returns a value x that is not equal to zero, and B.CompareTo (C) returns a value y of the same sign as x, then A.CompareTo (C) is required to return a value of the same sign as x and y ( A < B and B < C  A < C; same for >)