Generics allow you to define the specification of the data type of programming elements in a class or a method, until it is actually used in the program.
In other words, generics allow you to write a class or method that can work with any data type.
Example
class Program
{
static void Main(string[] args)
{
int [] ar={10,20,30,50,80,60};
string [] ar1 = { "kul","amit","soham"};
Student.Show(ar);
Student.Show(ar1);
Console.ReadLine();
}
}
static class Student
{
public static void Show<T>(T [] ar)
{
for (int i = 0; i < ar.Length;i++ )
{
Console.WriteLine(ar[i]);
}
}
}
{
static void Main(string[] args)
{
int [] ar={10,20,30,50,80,60};
string [] ar1 = { "kul","amit","soham"};
Student.Show(ar);
Student.Show(ar1);
Console.ReadLine();
}
}
static class Student
{
public static void Show<T>(T [] ar)
{
for (int i = 0; i < ar.Length;i++ )
{
Console.WriteLine(ar[i]);
}
}
}
No comments:
Post a Comment