Uploaded on

Generics in dot net

Generics in dot net

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
504
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
12
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • Type Checking - When we added an Int to our collection of type List, there is an implicit cast to Object. Similarly, if a int object is retrieved from the list, it must be cast at run time from an Object reference to a int reference. This lack of type safety at compile time is both tedious for the developer and prone to error. In contrast, when we used generics and our List<itemType> where itemType is typed to int causes all add and lookup methods to work with int references. This allows for specifying and checking the type of the elements at compile time rather than at run time.
    Binary code reuse- For maintenance purposes, a developer may elect to achieve compile-time type safety using List by deriving a StringList from it. The problem with this approach is that new code has to be written for every type for which you want a type-safe list, which can quickly become laborious. With List<T>, all you need to do is instantiate the type with the desired element type as T. As an added value, generics code is generated at run time, so two expansions over unrelated element types such as List<String> and List<FileStream> are able to reuse most of the same just-in-time (JIT)-compiled code. The CLR simply works out the details—no more code bloat!
    Performance - The bottom line is this: if type checking is done at compile time rather than at run time, performance improves. In managed code, casts between references and values incur both boxings and unboxings, and avoiding such casts can have an equally negative impact on performance. Current benchmarks of a quick-sort of an array of one million integers shows the generic method is three times faster than the non-generic equivalent. This is because boxing of the values is avoided completely. The same sort over an array of string references resulted in a 20 percent improvement in performance with the generic method due to the absence of a need to perform type checking at run time.
    Clarity – Clarity with generics comes in a number of forms. Constraints are a feature in generics that have the effect of making incompatible expansions of generic code impossible; with generics, you're never faced with the cryptic compiler errors that plague C++ template users. In the GenericSortedList<T> example, the collection class would have a constraint that makes it only work with types for T that can be compared and therefore sorted. Also, generic methods can often be called without employing any special syntax by using a feature called type inference. And, of course, type safety at compile time increases clarity in application code. I will look at constraints, type inference, and type safety in detail throughout this column.

Transcript

  • 1. Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
  • 2. .NET Generics
  • 3. 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-time! run-time
  • 4. • 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
  • 5. 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); }
  • 6. Define and use a generic method public class Method<T> { T[] item; public void Test(T item) {...} } Method<int> Test= new Method<int>();
  • 7. Placeholders for Generic Types • ƒ T…type • ƒ K…key • ƒ V…value
  • 8. Applying Generics • Generics may have any # of type parameters Class 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
  • 9. 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
  • 10. 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>
  • 11. 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 parameter public class LinkedList<K, T> where K : IComparable<K> where T : IClonable
  • 12. 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
  • 13. Using Generics in the BCL
  • 14. Enhancing the Base Library Generic Collections • System.Collections.Generic classes – – – – – List<T> LinkedList<T> Dictionary<K, V> Stack<T> Queue<T> // new in Beta1! // equivalent to non-generic HashTable • System.Collections.Generic interfaces – – – – – – – IList<T> IDictionary<K, V> ICollection<T> IEnumerable<T> IEnumerator<T> IComparable<T> IComparer<T> // replacement for IList // replacement for IDictionary // replacement for ICollection // replacement for IEnumerable // replacement for IEnumerator // replacement for IComparable // replacement for IComparer
  • 15. 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(); }
  • 16. THANK YOU
  • 17. 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
  • 18. Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550