Introduction
Generics in C# are an extension to C# Programming using Generic Types and Methods. I see most of the developers still get confused about generics and do not know to use them in real time projects. That made me to write this article to help understand and refer how to use them?
Basic concept is similar to Templates in C++ with better extensibility and usability. The C++ Templates are compile time Feature whereas Generics is a feature of runtime.
These are a type of data structure that contains code, remains the same. The data type of the parameters will change for each use. We can also call them parameterized types or parametric polymorphism. Generics allow classes, structs, interfaces, delegates, and methods to be parameterized by the types of data they store.
The usage within the data structure adapts to the different data type of the passed variables. This Allows defining type-safe data structures need less committing to actual data types. We reuse data processing algorithms without duplicating type-specific code.
System.Collections.Generic
The System.Collections.Generic namespace contains several generic collection classes based on generics and it is recommended that we should use these collection classes in place of the earlier non-generic ones for better performance of our applications.
The namespace contains a lot of generic classes, structures, and interfaces like
Dictionary
List
Queue
SortedDictionary
Stack
Examples
view sourceprint?
public class Gen<T>
{
T t;
public T Val
{
get;
set;
}
}
The class name " Gen<T” represents that the Type is generic, specifically the brackets <>containing the Type. This Type "T" is used to show that if we need to refer to the actual Type that is going to be used when we write this class".
T t; Creates member of the type T
The generic Type that we will specify during construction of the class will get inserted by the Common Language Runtime (CLR). The final item in the class is the public property. Again, notice that we are using the Type placeholder "T" to represent that generic type for the type of that property. Also notice that we can freely use the private variable "t" within the class.
In order to use this class to hold any Type, we simply need to create a new instance of our new Type, providing the name of the Type within the "<>" brackets and then use that class in a Type-safe manner as shown below.
view sourceprint?
static void Main(string[] args)
{
//create a string version of our generic class
Gen str = new Gen();
//set the value for the string type
str.Val = "Beyond Relational";
//output the type and value
System.Console.WriteLine(str.Val);
System.Console.WriteLine(str.Val.GetType());
//create another instance of our generic class, using a different type
Gen flt = new Gen();
flt.Val = 12.34;
System.Console.WriteLine(flt.Val);
//output
System.Console.WriteLine(flt.Val.GetType());
}
}
Generic Collections
view sourceprint?
public class MessageElement
{
//some functionality
}
public class MessageSearchCriteria
{
// some functioanlity
}
public List ListMessageElement
{
get
{
return lstMsgElemet;
}
set
{
lstMsgElemet = value;
}
}
MessageSearchCriteria objMsgSearch = new MessageSearchCriteria();
//usage of generics
objMsgSearch.ListMessageElement = new List();
Advantages
Help to make the code in the software components much more reusable.
High Quality Code and Code Maintainability
No runtime Casting No Boxing hence Allows Performance Efficiency
Creates Type safe at compile time as generics specify type at runtime
Programmer has stronger and flexible control over the code
The stronger type checking associated with generics helps in finding errors easily at compile time only.
No code Duplication
Generics refer to classes and methods that work consistently on values of different types.
Best Practices to use
If the data type contains Collection Type or any unspecified data types, then create a generic type.
If the data type will be operating on value types, then using generics will prevent the boxing and un boxing operations.
If the code contains multiple classes to handle different data types on which they operate then it is best practice to use Generics.
0 comments:
Post a Comment