Generic allows you to delay writing the specification of the data type of the programming element in the class or 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.
Using generics is a technology that enhances program functions, specifically in the following aspects:
It helps you to reuse code to the maximum extent, protect type safety, and Improve performance.
You can create generic collection classes. The .NET framework class library contains some new generic collection classes in the System.Collections.Generic namespace. You can use these generic collection classes to replace the collection classes in System.Collections.
You can create your own generic interfaces, generic classes, generic methods, generic events and generic delegates.
You can restrict generic classes to access methods of specific data types.
Information about the types used in generic data types can be obtained by using reflection at runtime.
Generics can be delegates or methods
using< span style="color: #000000;"> System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace Coding exercises
{
public class MyGenericeArry{
private T[] arr {get; set;}
public MyGenericeArry(int size){
arr = new T[size];
}
public T GetItem(int index) {
return arr[index];
}
public void SetItem(int index,T value) {
arr[index] = value;
}
}
public class Start {
public static void Main(string[] args)
{
MyGenericeArry<int> intarr = new MyGenericeArry<< span style="color: #0000ff;">int>(5);
for (int i = 0; i <5; i++)
{
intarr.SetItem(i,i*5);
}
for (int i = 0; i <5; i++)
{
Console.WriteLine(intarr.GetItem(i));
}
MyGenericeArry<char> chararr = new MyGenericeArry<< span style="color: #0000ff;">char>(5);
for (int i = 0; i <5; i++)
{
chararr.SetItem(i, 'a');
}
for (int i = 0; i <5; i++)
{
Console.WriteLine(chararr.GetItem(i));
}
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace Coding exercises
{
public class Genswap {
public static void Swap(ref T a1,ref T a2) {
T temp;
temp = a1;
a1 = a2;
a2 = temp;
}
public static void Main(string[] args)
{
int a, b;
char c, d;
a = 10;
b = 5;
c = 'a';
d = 'b';
Swap<int>(ref a,ref b);
Swap<char>(ref c, ref d);
Console.WriteLine(string.Format($" {a}{b}"));
Console.WriteLine(string.Format($" {c}{d}"));
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace Coding exercises
{
public class MyGenericeArry{
private T[] arr {get; set;}
public MyGenericeArry(int size){
arr = new T[size];
}
public T GetItem(int index) {
return arr[index];
}
public void SetItem(int index,T value) {
arr[index] = value;
}
}
public class Start {
public static void Main(string[] args)
{
MyGenericeArry<int> intarr = new MyGenericeArry<< span style="color: #0000ff;">int>(5);
for (int i = 0; i <5; i++)
{
intarr.SetItem(i,i*5);
}
for (int i = 0; i <5; i++)
{
Console.WriteLine(intarr.GetItem(i));
}
MyGenericeArry<char> chararr = new MyGenericeArry<< span style="color: #0000ff;">char>(5);
for (int i = 0; i <5; i++)
{
chararr.SetItem(i, 'a');
}
for (int i = 0; i <5; i++)
{
Console.WriteLine(chararr.GetItem(i));
}
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace Coding exercises
{
public class Genswap {
public static void Swap(ref T a1,ref T a2) {
T temp;
temp = a1;
a1 = a2;
a2 = temp;
}
public static void Main(string[] args)
{
int a, b;
char c, d;
a = 10;
b = 5;
c = 'a';
d = 'b';
Swap<int>(ref a,ref b);
Swap<char>(ref c, ref d);
Console.WriteLine(string.Format($" {a}{b}"));
Console.WriteLine(string.Format($" {c}{d}"));
}
}
}