Presentation is loading. Please wait.

Presentation is loading. Please wait.

Generics Generics vs. heterogeneous collections Doing your own generics FEN 2014UCN Teknologi/act2learn1.

Similar presentations


Presentation on theme: "Generics Generics vs. heterogeneous collections Doing your own generics FEN 2014UCN Teknologi/act2learn1."— Presentation transcript:

1 Generics Generics vs. heterogeneous collections Doing your own generics FEN 2014UCN Teknologi/act2learn1

2 ”Generic” Programming without generics in C# (as it was until Summer 2005 – and you still see it, also in other languages) All classes inherit from Object So we can apply polymorphism and use Object as static type for elements in containers For instance: Object[ ] data –this array may take any object as element –This approach is well known from standard collections as ArrayList, HashTable etc. FEN 2014UCN Teknologi/act2learn2

3 Pros and Cons Pros –heterogeneous collections –... Cons –many type casts –not type safe type checking is done runtime when casting –int and other native (value) type must be wrapped. (boxing – costs runtime overhead) Is this really an advantage? FEN 2014UCN Teknologi/act2learn3

4 The Idea: Types as Parameters C# before 2005: ArrayList al = new ArrayList(); Customer c= (Customer)al[i];//cast Instead we want something like: List al = new List (); Customer c= al[i]; – The compiler is able to check that only objects with static type Customer is placed in al – So the compiler knows that everything that may come out from al has static type Customer –So static type checking instead of dynamic type checking is possible –Dynamic casting can be avoided (but is not in all implementations) Type parameter FEN 2014UCN Teknologi/act2learn4 And that’s what we got

5 oldFashion: EmpSeqIteratorEmpSeqIterator Employee a1 = new Employee("Joe", "Programmer", 10000); Employee a = new Employee("Curt", "Senior Programmer", 20000); Employee b = new Employee("Carl", "Programmer", 10000); Employee c = new Employee("Karen", "System Programmer", 13000); Employee d = new Employee("Lisa", "Programmer", 11000); Employee e = new Employee("John", "System Engineer", 9000); string s = "HELLOOOO!"; ArrayList emps = new ArrayList(); emps.Add(a1); emps.Add(a); emps.Add(b); emps.Add(c); emps.Add(d); emps.Add(e); emps.Add(s); FEN 2014UCN Teknologi/act2learn5 for(int i = 0; i < emps.Count - 1; i++) Console.WriteLine(((Employee)emps[i]).Name); Console.WriteLine(); Explicit cast is necessary, but it works fine. No compiler error!

6 oldFashion: EmpSeqIteratorEmpSeqIterator Employee a1 = new Employee("Joe", "Programmer", 10000); Employee a = new Employee("Curt", "Senior Programmer", 20000); Employee b = new Employee("Carl", "Programmer", 10000); Employee c = new Employee("Karen", "System Programmer", 13000); Employee d = new Employee("Lisa", "Programmer", 11000); Employee e = new Employee("John", "System Engineer", 9000); string s = "HELLOOOO!"; ArrayList emps = new ArrayList(); emps.Add(a1); emps.Add(a); emps.Add(b); emps.Add(c); emps.Add(d); emps.Add(e); emps.Add(s); FEN 2014UCN Teknologi/act2learn6 foreach (Employee ans in emps) Console.WriteLine(ans.Name); Console.WriteLine(); Implicit cast No compiler error! Runtime exception at string

7 withGenerics: EmpSeqIteratorEmpSeqIterator Employee a1 = new Employee("Joe", "Programmer", 10000); Employee a = new Employee("Curt", "Senior Programmer", 20000); Employee b = new Employee("Carl", "Programmer", 10000); Employee c = new Employee("Karen", "System Programmer", 13000); Employee d = new Employee("Lisa", "Programmer", 11000); Employee e = new Employee("John", "System Engineer", 9000); string s = "HELLOOOO!"; IList emps = new List (); emps.Add(a1); emps.Add(a); emps.Add(b); emps.Add(c); emps.Add(d); emps.Add(e); emps.Add(s); //COMPILER ERROR!!!! FEN 2014UCN Teknologi/act2learn7

8 Do your own container homeMade: EmpSeqIteratorEmpSeqIterator Encapsulate all the casts: FEN 2014UCN Teknologi/act2learn8 interface ISeqEmps : IEnumerable { void insert(int i, Employee e); //Pre: 0 <= i <= length() //Post: e is insert at position i in the sequence // Succeeding elements are move to oldPos+1 Employee inspect(int i); //Pre: 0 <= i < length() //Post: the element at position i is returned void update(int i, Employee e); //--- IEnumerable GetReverseEnumerator(); We would like to have an iterator and use foreach We may want to create an iterator of our own

9 Do your own container homeMade: EmpSeqIteratorEmpSeqIterator Encapsulate all the casts: FEN 2014UCN Teknologi/act2learn9 class SeqEmps: ISeqEmps { private ArrayList l; public void insert(int i, Employee e){ l.Insert(i,e); } public Employee inspect(int i){ return (Employee)l[i]; } public IEnumerator GetEnumerator(){ return l.GetEnumerator(); } public IEnumerable GetReverseEnumerator(){ for (int i = l.Count; i > 0; i--) yield return l[i - 1]; } //--- } Return the ArrayList iterator Return our own iterator

10 Implementation of Generics “Macro-expansion” (C++ template): –Static checking, no casts, larger executables. Encapsulating the casts (like “homeMade”, Java): –Static checking, dynamic cast C#: –C++ way for value types –Java way for reference types FEN 2014UCN Teknologi/act2learn10

11 Do your own generic container: MyListProject MyListProject FEN 2014UCN Teknologi/act2learn11 public class MyList : IEnumerable, ICollection { List list; int n;//number of elements public MyList(int size) { list= new List (); n= 0; } You may want your container to fit into the Collections API Decide on an data representation. Use the type parameter as type Define the class with a type parameter

12 Do your own generic container: MyListProject MyListProject FEN 2014UCN Teknologi/act2learn12 public void Add(T o){ list.Add(o); n++; } public T Get(int i){ return list[i]; } public IEnumerator GetEnumerator(){ return list.GetEnumerator(); } public bool Contains(T item){ throw new NotImplementedException(); } Implement the methods (all of them )

13 Do your own generic container: MyListProject MyListProject FEN 2014UCN Teknologi/act2learn13 static void Main(string[] args) { MyList l= new MyList (10); l.Add(1); l.Add(2); l.Add(3); Console.WriteLine("Count : "+ l.Count); foreach(int x in l) Console.WriteLine(x); Console.ReadLine(); } Use it like any other generic collection

14 Do your own generic container: MyListProject MyListProject FEN 2014UCN Teknologi/act2learn14 You may want to do your own iterator In MyList

15 Generics with constrains on type parameters It’s possible to specify that the type parameter should implement certain interfaces: Only key types (K) that implements IComparable are allowed. FEN 2014UCN Teknologi/act2learn15 public class LinkedList where K : IComparable { T Find(K key) { Node current = m_Head; while(current.NextNode != null) { if(current.Key.CompareTo(key) == 0) break; else current = current.NextNode; } return current.Item; } //Rest of the implementation } // from: http://msdn.microsoft.com


Download ppt "Generics Generics vs. heterogeneous collections Doing your own generics FEN 2014UCN Teknologi/act2learn1."

Similar presentations


Ads by Google