Generics & Collection Classes Version 1.0. Topics Generic Methods and Classes Generic Collection...

download Generics & Collection Classes Version 1.0. Topics Generic Methods and Classes Generic Collection Classes List Enumerators Queue Stack LinkedList.

If you can't read please download the document

description

Objectives After completing this topic, students should be able to: Correctly design and use generic methods and generic classes Correctly design and use classes that use the generic collection classes in a C# program * Know how to correctly use an List * know how to correctly use a Queue * Know to correctly use a Stack * Know how to correctly use a LinkedList * Know how to correctly use an enumerator

Transcript of Generics & Collection Classes Version 1.0. Topics Generic Methods and Classes Generic Collection...

Generics & Collection Classes Version 1.0 Topics Generic Methods and Classes Generic Collection Classes List Enumerators Queue Stack LinkedList Objectives After completing this topic, students should be able to: Correctly design and use generic methods and generic classes Correctly design and use classes that use the generic collection classes in a C# program * Know how to correctly use an List * know how to correctly use a Queue * Know to correctly use a Stack * Know how to correctly use a LinkedList * Know how to correctly use an enumerator Generic Methods In a normal method we use parameters to tell the method what values to use. A Generic Method lets us parameterize the type of data that the method will use. Example Assume that you want to have method that swaps two int values so you can use it to sort an array of int types. static void Swap(ref int lhs, ref int rhs) { int tempVal = lhs; lhs = rhs; rhs = tempVal; } int[] iArray = { 30, 100, 10, 200, 5, 500 }; Swap(ref iArray[0],ref iArray[1]); Make it Generic Now what if you want to generalize the method so that it swaps two values of any type. static void Swap (ref T lhs,ref T rhs) { T tempVal = lhs; lhs = rhs; rhs = tempVal; } int[] iArray = { 30, 100, 10, 200, 5, 500 }; Swap(ref iArray[0],ref iArray[1]); Add to method name Make it Generic static void Swap (ref T lhs,ref T rhs) { T tempVal = lhs; lhs = rhs; rhs = tempVal; } int[] iArray = { 30, 100, 10, 200, 5, 500 }; Swap(ref iArray[0],ref iArray[1]); Change int to T Just like you can create a generic method, it is possible to create a generic class. One or more of the data members of a generic class will be parameterized. Generic Class Example A box of class Box { private T theContent;... } The class is parameterized with It has a data member of type T Create a Box containing an int like this Box boxOfInt = new Box (6); The data type is int Create a Box containing a double like this Box boxOfInt = new Box (6.5); The data type is double Class Constructor public Box(T theParameter) { theContent = theParameter; } data type is parameterized A Getter and a Setter public T getContent() { return theContent; } public void setContent(T theParameter) { theContent = theParameter; } returns data of type T Takes a parameter of type T The Complete Class class Box { private T theContent; public Box(T theParameter) { theContent = theParameter; } public T getContent() { return theContent; } public void setContent(T theParameter) { theContent = theParameter; } static void Main() { Box boxOfInt = new Box (6); boxOfInt.setContent(6); Console.WriteLine(boxOfInt.getContent()); Console.ReadLine(); } A Small Driver A More Complex Example Create a dynamic array of Ts A More Complex Example class DArray { private static int DSIZE = 5; private T[] _TArray; private T[] _TempArray; private int _Capacity; private T _TempVal; public Capacity //property public Count//property public Darray() //constructor public void Swap(int,int) //method private void Resize() //method public void AddData(T) //method public T GetData() //method } class Darray { private static int DSIZE = 5; private T[] _TArray; private T[] _TempArray; private int _Capacity; public int Capacity { get { return _Capacity; } } private int _Count; public int Count { get { return _Count; } } private T _TempVal; public DArray() { _Capacity = DSIZE; _Count = 0; _TArray = new T[_Capacity]; } public void Swap(int fval,int sval) { _TempVal = _TArray[fval]; _TArray[fval] = _TArray[sval]; _TArray[sval] = _TArray[fval]; } private void Resize() { _TempArray = new T[_Capacity * 2]; _TArray.CopyTo(_TempArray, 0); _TArray = _TempArray; _TempArray = null; _Capacity *= 2; } public void AddData(T aval) { if (_Count >= _Capacity) Resize(); _TArray[_Count++] = aval; } public T GetData(int pos) { if (pos >= 0 && pos