using System; using System.Collections.Generic; namespace Unit_18_Generics { public class Generics //Generic class { public void Swap(ref K lhs, ref K rhs) { //Generic method Console.WriteLine("You sent the Swap() method a {0}", typeof(K)); K temp;//Local generic data type temp = lhs; lhs = rhs; rhs = temp; } //Displays the data type and its base type public void DisplayBaseClass() { Console.WriteLine("Base class of {0} is: {1}.", typeof(K), typeof(K).BaseType); } } class Tester { static void Main(string[] args) { //Service //Adding two integars Generics g = new Generics(); int a = 2, b = -20; Console.WriteLine("Before swap: {0} , {1}", a, b); g.Swap(ref a, ref b); Console.WriteLine("After swap: {0} , {1}", a, b); g.DisplayBaseClass(); //Adding two strings Generics gb = new Generics(); string strA = "Hello", strB = "David"; Console.WriteLine("\nBefore swap: {0} {1}", strA, strB); g.Swap(ref strA, ref strB); Console.WriteLine("After swap: {0} {1}", strA, strB); g.DisplayBaseClass(); Console.Read(); } } }