Generics in C#
Generics are that sort of classes which has a PlaceHolder instead of data types. When you make generic classes you don't indicate its information type. Datatypes are characterized when you make objects. This makes Generic classes reusable and type-safe and your Generic classes and techniques can work with any datatypes. For the most part, Generics are utilized for making a collection of classes. You can utilize Generics by including System.Collections.Generic namespace and also you can make your own Generic interfaces, classes, techniques, events, and delegates. When we are creating a generic class, we should use angle < > brackets which utilized to declare a class or strategy as a generic type. So let's see the program which demonstrates Generics class as follows.
Program
using System;
namespace program
{
public class Generic<Gen_list>
{
public void GenFunction(Gen_list display)
{
Console.WriteLine(display);
}
}
public class Program
{
static void Main(string[] args)
{
/*To get string value*/
Console.WriteLine("String Value");
Generic<string> Getstring=new Generic<string>();
Getstring.GenFunction("Hello world\n");
/*To get int value*/
Console.WriteLine("Integer Value");
Generic<int> Get = new Generic<int>();
Get.GenFunction(450);
/*To get charater value*/
Console.WriteLine("Character value");
Generic<char> Getchar = new Generic<char>();
Get.GenFunction('A');
Console.ReadKey();
}
}
}
Output
Generics are that sort of classes which has a PlaceHolder instead of data types. When you make generic classes you don't indicate its information type. Datatypes are characterized when you make objects. This makes Generic classes reusable and type-safe and your Generic classes and techniques can work with any datatypes. For the most part, Generics are utilized for making a collection of classes. You can utilize Generics by including System.Collections.Generic namespace and also you can make your own Generic interfaces, classes, techniques, events, and delegates. When we are creating a generic class, we should use angle < > brackets which utilized to declare a class or strategy as a generic type. So let's see the program which demonstrates Generics class as follows.
Program
using System;
namespace program
{
public class Generic<Gen_list>
{
public void GenFunction(Gen_list display)
{
Console.WriteLine(display);
}
}
public class Program
{
static void Main(string[] args)
{
/*To get string value*/
Console.WriteLine("String Value");
Generic<string> Getstring=new Generic<string>();
Getstring.GenFunction("Hello world\n");
/*To get int value*/
Console.WriteLine("Integer Value");
Generic<int> Get = new Generic<int>();
Get.GenFunction(450);
/*To get charater value*/
Console.WriteLine("Character value");
Generic<char> Getchar = new Generic<char>();
Get.GenFunction('A');
Console.ReadKey();
}
}
}
Output