Generics tutorial in C#.Net

This is tutorial about Generics in C#.Net. You can also check my previous posts on:

Log4Net in asp.net

Namespace in C#.Net

Asp.Net page lifecycle events.

Generics allow creating classes that support any type. Basically, it creates a class template 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 compile-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);


You May Also like the Following SharePoint Online Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →

Leave a Reply