static void Main(string[] args) { GenericWhere.Sample(); GenericMethod.Sample(); GenericClass.Sample(); GenericInterface.Sample(); GenericArray.Sample(); GenericDelegate.Sample(); GenericDefault.Sample(); }
static void Main(string[] args) { //instanciating class or declaring the array //MyGenericArray obj = new MyGenericArray(5); ////setting the values //for(int i=0;i<5;i++) //{ // obj.setItem(i, i * 5); //} ////retrieving the values //for (int i = 0; i < 5; i++) //{ // Console.Write(obj.getItem(i) + " "); //} //Generic class MyGenericArray <int> objIntArray = new MyGenericArray <int>(5); //setting the values for (int i = 0; i < 5; i++) { objIntArray.setItem(i, i * 5); } //retrieving the values for (int i = 0; i < 5; i++) { Console.Write(objIntArray.getItem(i) + " "); } Console.WriteLine(); Console.WriteLine("Entering characters"); //declaring a character array MyGenericArray <char> objCharArray = new MyGenericArray <char>(5); //setting the values for (int i = 0; i < 5; i++) { objCharArray.setItem(i, (char)(i + 97)); } //retrieving the values for (int i = 0; i < 5; i++) { Console.Write(objCharArray.getItem(i) + " "); } Console.WriteLine(); //non-Generic method GenericMethod objMethod = new GenericMethod(); int a = 10; int b = 20; char c = 'S'; char d = 'M'; Console.WriteLine("Values before swapping"); Console.WriteLine("a={0},b={1}", a, b); Console.WriteLine("c={0},d={1}", c, d); objMethod.swapInt(ref a, ref b); objMethod.swapChar(ref c, ref d); Console.WriteLine("Values after swapping"); Console.WriteLine("a={0},b={1}", a, b); Console.WriteLine("c={0},d={1}", c, d); //generic method Console.WriteLine("Values before swapping in Genreric"); Console.WriteLine("a={0},b={1}", a, b); Console.WriteLine("c={0},d={1}", c, d); objMethod.swapGeneric <int>(ref a, ref b); objMethod.swapGeneric <char>(ref c, ref d); Console.WriteLine("Values after swapping in Generic"); Console.WriteLine("a={0},b={1}", a, b); Console.WriteLine("c={0},d={1}", c, d); Console.WriteLine(); Console.WriteLine("Generic with 2 methods"); objMethod.swapGeneric1 <int, string>(5, "Sachin"); objMethod.swapGeneric1 <int, int>(5, 6); Console.WriteLine(); Console.ReadLine(); }
static void Main(string[] args) { try { Console.WriteLine(typeof(List <>)); //`1 占位符 1个参数 Console.WriteLine(typeof(Dictionary <,>)); //`2 int iValue = 123; // var iValue = 123; string sValue = "456"; DateTime dtValue = DateTime.Now; object oValue = "Jonty"; Console.WriteLine("*************************"); CommonMethod.ShowInt(iValue); CommonMethod.ShowString(sValue); CommonMethod.ShowDateTime(dtValue); CommonMethod.ShowObject(oValue); // 全部用ShowObject CommonMethod.ShowObject(sValue); CommonMethod.ShowObject(iValue); CommonMethod.ShowObject(dtValue); Console.WriteLine("************Generic***********"); GenericMethod.Show <int>(iValue); // 可省略 自动推断 GenericMethod.Show(sValue); GenericMethod.Show <DateTime>(dtValue); Console.WriteLine("*************Monitor************"); // common~=generic > object Monitor.Show(); Generic <int> generic = new Generic <int>() { _t = 123 }; { People people = new People() { Id = 12, Name = "走自己的路" }; Chinese chinese = new Chinese() { Id = 34, Name = "Jonty" }; HuNan huNan = new HuNan() { Id = 45, Name = "长沙" }; Japanese japanese = new Japanese() { Id = 56, Name = "波多" }; CommonMethod.ShowObject(people); CommonMethod.ShowObject(chinese); CommonMethod.ShowObject(huNan); CommonMethod.ShowObject(japanese); GenericMethod.Show(people); GenericMethod.Show(chinese); GenericMethod.Show(huNan); GenericMethod.Show(japanese); } } catch (Exception e) { Console.WriteLine(e.Message); throw; } }