C# Generics
Introduction
This article explains about the basic advantages of generic types and how their use can improve type safety, code reuse, and performance. Also we get a taste of the syntax in C# and see how generics guide to an additional point of indirection, consequential in better flexibility. A C# programmer can be much stronger and flexible technology to use in their code without any hesitation.
Generics
Generics mean classes and methods that work homogeneously on values of different types. They are a type of data structure that contains code that remains the same; though, the data type of the parameters can change with each use. We can also call them parameterized types or parametric polymorphism. They are easy and effective to use.
The use
1. Briefly, a generic is a code template that can be applied to use the same code over and over again.
2. The usage of generics within the data structure adjusts to the different data type of the passed variables.
3. Both the casting and the boxing and unboxing operations degrade
Performance in Generics.
4. Generics let us to specify our Type at runtime.
5. You can create a collection that is type-safe at compile-time in Generics.
6. Code clearness with generics is another advantage.
7. There is use of list collection as their new type not as objects in Generics.
8. Binary code is reused in Generics.
9. It gives no boxing, no casting advantage.
10. Whenever the generic is used, it can be adapted for different data types without require to rewrite any of the internal code.
Example:
List Mylist1 = new List();
// No boxing or casting necessary
list1.Add (5);
By expanding this code and make it more sensible, we can build it in thisv way:
using System;
using System.Collections;
using System.Text;
namespace TList
{
public class List : CollectionBase
{
public List(){ }
public Template this[int index]
{
get { return (Template)List[index]; }
set { List[index] = value; }
}
public int Add(Template value)
{
return List.Add(value);
}
}
}
11. The generic List doesn't need any fields, but assume a private integer field named test is exist that we would like to make it generic. If we decide to convert any fields to generic fields, we would just change their types to Template.
See a sample code here:
private Template test;
Advantages
1. A generic collection can be created and can be handled any Type in a generic and Type-Safe approach. For example, we can have a single array class to store a list of Employees or yet a list of Items, and as we actually use it, we will be able to access the items in the collection directly as a list of Employees or Items, and not as objects.
2. Generics allow classes, structs, interfaces, delegates, and methods to be parameterized by the types of data they store and manipulate.
3. We can refer to a class, where we don't force it to be related to any specific Type, except we can perform work with it in a Type-Safe advance.
Conclusion
Most of the developers are perplexed on C# Generics. There is no reason for it. If your background comes from C++ or Java you may understand more easily. Main concept of C ++ and Java is the same in C# with some better extensions and easy use. Eg: since C++ Templates uses compile time, C# Generics also a feature of the runtime. They are basically the capacity to have type parameters lying on your type.