Download - Generics in dot net
Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
.NET Generics
Generics
• Using Generics, we can create class templates that support any type.
• Compile-time type checking– By creating a generic class, you can create a collection that
is type-safe at compile-time.– There is no need to write code to test for the correct data
type, because it is enforced at compile time– The need for type casting and the possibility of run-time
errors are reduced.• Binary code reuse– Reuse same code with multiple types– Generic code generated at run-timerun-time!
• Generics are classes, structures, interfaces, and methods that have placeholders (type parameters) for one or more of the types that they store or use.
• A generic collection class might use a type parameter as a placeholder for the type of objects that it stores
• improved performance • reduced code
simple generic class definition
• public class Generic<T> { public T Field; }
public static void Main() { Generic<string> g = new Generic<string>(); g.Field = "A string"; Console.WriteLine("Generic.Field = \"{0}\"", g.Field); Console.WriteLine("Generic.Field.GetType() = {0}", g.Field.GetType().FullName); }
Define and use a generic method
public class Method<T> { T[] item; public void Test(T item) {...} } Method<int> Test= new Method<int>();
Placeholders for Generic Types
• T…type �• K…key �• V…value�
Applying Generics
• Generics may have any # of type parametersClass Node<K, T>
{ public Node(K key, T item, Node<K, T>nextNode {….}
private K Key;
private T Item;
private Node<k, T> NextNode; }
• Must then instantiate with the # of parameters
Generic Interfaces
public interface ICalculator<T>
{
T Add(T param1, T param2);
}
public class SamCalculator : ICalculator<int>
{
public int Add(int arg1, int ar2)
{ return arg1 + arg2; }
}
• Interface declared with type parameters = a generic interface declaration
Derivation Constraints
• Where reserved word to define a constraint• Use where on generic type parameter followed by derivation
colon indicating generic type parameter implements particular interface
public class LinkedList<K, T> where K : IComparable
• Overcome by new generic interface IComparable<T> in System.Collections.Generics
• Constrain plus new interface eliminates boxing penalty:public class LinkedList<K, T> where K : IComparable<K>
Derivation Constraints
• All constraints must appear after actual derivation list of generic class
• Ex: If LinkedList<T> derives from IEnumerable<T>, put where keyword after it:
public class LinkedList<K,T> : IEnumerable<T> where K :
IComparable<K>
• Can provide constraints for every generic type parameterpublic class LinkedList<K, T> where K : IComparable<K>
where T : IClonable
Constructor Constraints
• Suppose want to instantiate new generic object inside generic class– C# compiler doesn’t know whether specific type you are going to use
has matching public default constructor– Will not compile
• Constructor constraint makes possible for generic code to create instances of type specified by constraining type args to types that implement public default constructor
• Only default or parameterless constructors supported
Using Generics in the BCL
Enhancing the Base LibraryGeneric Collections
• System.Collections.Generic classes– List<T>– LinkedList<T> // new in Beta1!– Dictionary<K, V> // equivalent to non-generic HashTable– Stack<T>– Queue<T>
• System.Collections.Generic interfaces– IList<T> // replacement for IList– IDictionary<K, V> // replacement for IDictionary– ICollection<T> // replacement for ICollection– IEnumerable<T> // replacement for IEnumerable– IEnumerator<T> // replacement for IEnumerator– IComparable<T> // replacement for IComparable– IComparer<T> // replacement for IComparer
Using Generics in the BCL
• All generic collections implement generic IEnumerable<T> interface
• IEnumerable<T> provides access to IEnumerator<T> Iterators interface, for abstracted iterations over collections (Think C++ STL Iterators)
Public interface IEnumerable<T>
{ IEnumerator<T> GetEnumerator(); }
Public interface IEnumerator<T> : IDisposable
{ T Current {get;}
bool MoveNext(); }
THANK YOU
If this presentation helped you, please visit our page facebook.com/baabtra and like it.
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
Contact Us
Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550
NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550
Start up VillageEranakulam,Kerala, India.
Email: [email protected]