Generics In and Out

Jaliya Udagedara
What are we going to discuss today?
• Non-Generic Collections
• Demo 1 : Non-Generic Collections
• Generic Collections
• Generic List<T> Class
• Generic IEnumerable<T> Interface
• Constraints on Type Parameter (T)
• Demo 2 : Generic Collections (List<T>)
• Demo 3 : Creating Custom Generic Class and more.

• Advantages of Generics
• Some Recommandations
Collections

ICollection

IList

IDictionary
Non-Generic Collections
• System.Collections (Not included in the default C#
template)
• Examples
• ArrayList
• HashTable etc.
• Store objects
• Add anything.

• Typecast on removal.
Demo 1
ArrayList and HashTable
Generic Collections
• System.Collections.Generic (Included in the default C#
template)
• Examples
• List<T>
• Dictionary<TKey, TValue> etc.
• Store objects
• Add item of specific type.

• No need to box/unbox.
Generic Collections contd.
• Generic Classes
• Generic Interfaces
• Generic Methods
• Generic Delegates
The Generic List Class (List<T>)
public class List<T> : IList<T>, ICollection<T>,
IList, ICollection, IReadOnlyList<T>,
IReadOnlyCollection<T>, IEnumerable<T>,
IEnumerable
• List<T> is the generic List class

• T represents the type parameter to be supplied in
declarations.
• Provides traditional list operations

• Insert/Delete
List<T> Methods
• Add (T item)
• Add item at end of list
• Insert (int index, T item)
• Insert item at a specific position
• Remove (T item)
• Remove first occurrence of item
• RemoveAt (int index)

• Remove item at specified position
List<T> Methods contd.
• Clear()
• Removes all items from the list
• bool Contains(T item)
• Determines if item is in the list
• int IndexOf(T item)
• Returns index of item, or -1 if not in list.
• Sort()
• Array.Sort method
• Insertion Sort / Heap Sort or Quick Sort
• …more
The Generic IEnumerable<T> Interface
public interface IEnumerable<out T> : IEnumerable

• Exposes the enumerator.
• Simple iteration over a collection of a specified type.
• List<T> implements IEnumerable<T>

• Methods
• GetEnumerator()
• A lot of extension methods (visit MSDN)
Generic Delegates
• What is a Delegate?
• Function Pointer in C++.
static void Method1(string s)
{
// body
}
static void Method2(int i)
{
// body
}
delegate void MyGenericDelegate<T>(T data);
Constraints on Type Parameter (T)
• where T : struct
• T must be a value type

• where T : class
• T must be reference type

• where T : <base class>
• T must be deriving from base class

• where T : <interface>
• T must be implementing the interface

• where T : new()
• T must have parameter less constructor
Demo 2
List<T>
Demo 3
Custom Generic Class, Generic
Methods, Type Constraints etc.
So what’s the advantage?

Reusable
Efficient

Type Safe (in compile time too)
Recommendations
• For all the applications which targets .NET 2.0 and above
use new generic collection classes instead of the older
non-generic counterparts.
• For most scenarios that require collection classes, the
recommended approach is to use the ones provided in
the .NET Framework class library.
• If the items in the collection are being added/removed
from different threads, use collections in the
System.Collections.Concurrent which is introduced with
.NET Framework 4.
Thank You!
http://www.jaliyaudagedara.blogspot.com/

Generics In and Out

  • 1.
    Generics In andOut Jaliya Udagedara
  • 2.
    What are wegoing to discuss today? • Non-Generic Collections • Demo 1 : Non-Generic Collections • Generic Collections • Generic List<T> Class • Generic IEnumerable<T> Interface • Constraints on Type Parameter (T) • Demo 2 : Generic Collections (List<T>) • Demo 3 : Creating Custom Generic Class and more. • Advantages of Generics • Some Recommandations
  • 3.
  • 4.
    Non-Generic Collections • System.Collections(Not included in the default C# template) • Examples • ArrayList • HashTable etc. • Store objects • Add anything. • Typecast on removal.
  • 5.
  • 6.
    Generic Collections • System.Collections.Generic(Included in the default C# template) • Examples • List<T> • Dictionary<TKey, TValue> etc. • Store objects • Add item of specific type. • No need to box/unbox.
  • 7.
    Generic Collections contd. •Generic Classes • Generic Interfaces • Generic Methods • Generic Delegates
  • 8.
    The Generic ListClass (List<T>) public class List<T> : IList<T>, ICollection<T>, IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable • List<T> is the generic List class • T represents the type parameter to be supplied in declarations. • Provides traditional list operations • Insert/Delete
  • 9.
    List<T> Methods • Add(T item) • Add item at end of list • Insert (int index, T item) • Insert item at a specific position • Remove (T item) • Remove first occurrence of item • RemoveAt (int index) • Remove item at specified position
  • 10.
    List<T> Methods contd. •Clear() • Removes all items from the list • bool Contains(T item) • Determines if item is in the list • int IndexOf(T item) • Returns index of item, or -1 if not in list. • Sort() • Array.Sort method • Insertion Sort / Heap Sort or Quick Sort • …more
  • 11.
    The Generic IEnumerable<T>Interface public interface IEnumerable<out T> : IEnumerable • Exposes the enumerator. • Simple iteration over a collection of a specified type. • List<T> implements IEnumerable<T> • Methods • GetEnumerator() • A lot of extension methods (visit MSDN)
  • 12.
    Generic Delegates • Whatis a Delegate? • Function Pointer in C++. static void Method1(string s) { // body } static void Method2(int i) { // body } delegate void MyGenericDelegate<T>(T data);
  • 13.
    Constraints on TypeParameter (T) • where T : struct • T must be a value type • where T : class • T must be reference type • where T : <base class> • T must be deriving from base class • where T : <interface> • T must be implementing the interface • where T : new() • T must have parameter less constructor
  • 14.
  • 15.
    Demo 3 Custom GenericClass, Generic Methods, Type Constraints etc.
  • 16.
    So what’s theadvantage? Reusable Efficient Type Safe (in compile time too)
  • 17.
    Recommendations • For allthe applications which targets .NET 2.0 and above use new generic collection classes instead of the older non-generic counterparts. • For most scenarios that require collection classes, the recommended approach is to use the ones provided in the .NET Framework class library. • If the items in the collection are being added/removed from different threads, use collections in the System.Collections.Concurrent which is introduced with .NET Framework 4.
  • 18.