We are using this program to swap any type of data using Generics.
using System;
using System.Collections.Generic;
namespace SwapingUsingGenerics
{
class Program
{
static void swap<T>(ref T a,ref T b)
{
T temp = a;
a = b;
b = temp;
}
static void Main(string[] args)
{
string s1 = "tushar";
string s2 = "patel";
int i = 10;
int j = 22;
Console.WriteLine("Before swapping:s1={0},s2={1}",s1,s2);
swap<string>(ref s1,ref s2);
Console.WriteLine("Before swapping:s1={0},s2={1}",s1,s2);
Console.WriteLine("Before swapping:i={0},j={1}",i,j);
swap<int>(ref i, ref j);
Console.WriteLine("after swapping:i={0},j={1}",i,j);
}
}
}
Note:- In this program we are swaping any type of data. Notice that we are using only one Generic type method.
No comments:
Post a Comment