This is tutorial about Generics in C#.Net.
Also check out:
- Implement delete functionality in Gridview in Asp.Net
- Bind dropdownlist using arraylist or hashtable in C#.Net
- Get control value using JavaScript with master page in Asp.Net
Generics allow to create classes that supports any type. Basically it creates a class templates that supports any type, And when you instantiate that class, you can specify what type you want to use.
Below is an example:
List<int> lst = new List<int> { 1, 2, 3 };
Here it is a List of interger type. It should take only integer values.
If you try to insert string values like below:
lst[0] = "AspDotNetHelp.com";
Then it will give a compaile time error, like can not implicitly vonvert type string to int.
Below is an example of a Generics class:
public class UserMaster
{
public Int64 UserID
{
get;
set;
}
public string Name
{
get;
set;
}
public string EmailID
{
get;
set;
}
}
UserMaster userMaster = new UserMaster();
userMaster.UserID = 1234;
userMaster.Name = "AspDotNetHelp";
userMaster.EmailID = "aspdotnethelp24@gmail.com";
List<> lstUserMaster = new List<UserMaster>();
lstUserMaster.Add(userMaster);
Also check out:
- Implement delete functionality in Gridview in Asp.Net
- Bind dropdownlist using arraylist or hashtable in C#.Net
- Get control value using JavaScript with master page in Asp.Net
Generics allow to create classes that supports any type. Basically it creates a class templates that supports any type, And when you instantiate that class, you can specify what type you want to use.
Below is an example:
List<int> lst = new List<int> { 1, 2, 3 };
Here it is a List of interger type. It should take only integer values.
If you try to insert string values like below:
lst[0] = "AspDotNetHelp.com";
Then it will give a compaile time error, like can not implicitly vonvert type string to int.
Below is an example of a Generics class:
public class UserMaster
{
public Int64 UserID
{
get;
set;
}
public string Name
{
get;
set;
}
public string EmailID
{
get;
set;
}
}
UserMaster userMaster = new UserMaster();
userMaster.UserID = 1234;
userMaster.Name = "AspDotNetHelp";
userMaster.EmailID = "aspdotnethelp24@gmail.com";
List<> lstUserMaster = new List<UserMaster>();
lstUserMaster.Add(userMaster);