This document discusses .NET generics. It explains that generics allow creating reusable class and method templates that can work with different data types. This avoids duplicate code and provides type safety. Some key points made:
- Generics use placeholders like <T> for type parameters to make classes and methods reusable for multiple types.
- The .NET Base Class Library enhanced generics support with collections like List<T> and interfaces like IEnumerable<T>.
- Constraints can be applied to generic types using "where" to restrict them to implement particular interfaces. This provides type safety.
2. 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
4. 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
5. • 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
6. 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);
}
7. Define and use a generic method
public class Method<T>
{
T[] item;
public void Test(T item)
{...}
}
Method<int> Test= new Method<int>();
9. 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
10. 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
11. 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>
12. 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
13. 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
15. 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
16. 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(); }
18. 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
19. 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
Notes de l'éditeur
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.