usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceMyGeneirc{internalclassProgram{staticvoidMain(string[] args){try{// 变量定义int iValue =123;string strValue ="456";DateTime dtValue = DateTime.Now;object oValue ="Mr Sorry";// 泛型方法调用 Console.WriteLine("****************Geneirc***********************");// 泛型方法解决,一个方法,满足不同参数类型,做相同的事情GeneircMethod.Show<int>(iValue);GeneircMethod.Show<string>(strValue);GeneircMethod.Show<DateTime>(dtValue);GeneircMethod.Show<object>(oValue);// 泛型类GenericClass<int> genericInt =newGenericClass<int>{ _t =19};// 约束Console.WriteLine("****************约束非约束***********************");People people =newPeople(){ Id =123, Name ="张三"};Chinese chinese =newChinese(){ Id =234, Name ="李四"};Hubei hubei =newHubei(){ Id =456, Name ="王五"};Japanese japanese =newJapanese(){ Id =678, Name ="田中"};// 无意义显示GeneircMethod.Show<People>(people);GeneircMethod.Show<Chinese>(chinese);GeneircMethod.Show<Hubei>(hubei);GeneircMethod.Show<Japanese>(japanese);}catch(Exception ex){Console.WriteLine(ex.ToString());}Console.ReadLine();}}}
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceMyGeneirc{internalclassProgram{staticvoidMain(string[] args){try{People people =newPeople(){ Id =123, Name ="张三"};Chinese chinese =newChinese(){ Id =234, Name ="李四"};Hubei hubei =newHubei(){ Id =456, Name ="王五"};Japanese japanese =newJapanese(){ Id =678, Name ="田中"};Console.WriteLine("****************约束基类***********************");//Constraint.Show<People>(people);Constraint.Show<Chinese>(chinese);Constraint.Show<Hubei>(hubei);//Constraint.Show<Japanese>(japanese); // 编译报错Console.WriteLine("****************约束接口***********************");Constraint.Get<Chinese>(chinese);}catch(Exception ex){Console.WriteLine(ex.ToString());}Console.ReadLine();}}}