Generic Interfaces C# Help

Using generics, you can define interfaces that define methods with generic parameters. In the linked list sample, you’ve already implemented the interface IEnumerable<T>, which defines a GetEnumerator (~method to return IEnumerator<T>. For many non-generic interfaces of .NET 1.0, new generic versions have been defined since .NET 2.0, for example IComparable<T>:

Img

the non-generic interface IComparable that requires an object with the CompareTo () method is implemented with the Person class to sort persons by LastName:

Img

When implementing the generic version, it is no longer necessary to cast the object to a Person:

Img

Posted on October 29, 2015 in Generics

Share the Story

Back to Top
Share This