|
Feb 19
|
sponsored links: |
What have been generics ?
Generics is a brand brand brand new judgment in .Net Framework. Creating a pick up by regulating generics helps we assimilate a most appropriate make make make make use of of of of of generics. For example, we need a pick up category which we can make make make make use of of of of to emanate type-safe collections of a Teacher as well as Student objects. In alternative words, a Teacher collection should include of objects of usually a Teacher class, as well as a Student collection should include of objects of usually a Student class.
In a prior versions of .Net Framework store pick up objects as System.Object type. As a result, we can make make make make use of of of of a singular pick up intent to store objects of any types. This comprises type-safety as well as involves a opening beyond since of a need for substantial as well as pithy sort cast of characters whilst adding or reception objects from collections.
.Net Framework 2.0 provides generics which we can make make make make use of of of of to emanate type-safe creations for both anxiety as well as worth types.
Implementation of Generics
The following formula e.g. shows how to exercise generics in C#. The formula e.g. defines an category called CommonData,which is used to emanate dual objects, a single to store a fibre worth as well as a alternative to store a boyant value. a CommonData class ensures sort reserve by usurpation a compulsory sort in a constructor. this implies which a worth specified for an intent of a CommonData class is of a same type as which specified whilst formulating a analogous object.
class program
{
static blank Main(string[] args)
{
CommonData<string>name = brand brand brand new CommonData<string>();
name.Value = “.Net Framework”;
CommonData<float>version = brand brand brand new CommonData<float>();
version.Value = 2.0F;
Console.Writeline(name.Value);
Console.Writeline(version.Value),
}
}
public category CommonData<T>
{
private T _data;
public T value
{
get
{
return this._data;
}
set
{
this._data = value;
}
}
}