在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
namespace GenericsTest { class Program { // https://www.cnblogs.com/dotnet261010/p/9034594.html static void Main(string[] args) { int iValue = 123; string sValue = "456"; DateTime dtValue = DateTime.Now; Console.WriteLine("----------------------------CommonMethod-------------------------------"); CommonMethod.showInt(iValue); CommonMethod.showString(sValue); CommonMethod.showDateTime(dtValue); Console.WriteLine("----------------------------Object-------------------------------"); // Object会出现装箱和拆箱,会损耗程序的性能 CommonMethod.showObject(iValue); CommonMethod.showObject(sValue); CommonMethod.showObject(dtValue); Console.WriteLine("----------------------------泛型-------------------------------"); GenericMethod.show<int>(iValue); GenericMethod.show<string>(sValue); GenericMethod.show<DateTime>(dtValue); Console.WriteLine("----------------------------三种方法执行同样的操作,比较用时长短-------------------------------"); // 从结果中可以看出:泛型方法的性能最高,其次是普通方法,object方法的性能最低。 MonitorMethod.Show(); Console.ReadKey(); } } } namespace GenericsTest { public class CommonMethod { /// <summary> /// 打印个int值 /// </summary> /// <param name="iParamenter"></param> public static void showInt(int iParameter) { Console.WriteLine("This is {0}, parameter = {1}, type = {2}.", typeof(CommonMethod).Name, iParameter.GetType().Name, iParameter); } /// <summary> /// 打印个string值 /// </summary> /// <param name="sParameter"></param> public static void showString(string sParameter) { Console.WriteLine("This is {0}, parameter = {1}, type = {2}.", typeof(CommonMethod).Name, sParameter.GetType().Name, sParameter); } //、打印个DateTime值 public static void showDateTime(DateTime dtParameter) { Console.WriteLine("This is {0}, parameter = {1}, type = {2}.", typeof(CommonMethod).Name, dtParameter.GetType().Name, dtParameter); } /// <summary> /// 上面的三个例子进行优化 /// </summary> /// <param name="oParameter"></param> public static void showObject(object oParameter) { Console.WriteLine("This is {0}, parameter = {1}, type = {2}.", typeof(CommonMethod).Name, oParameter.GetType().Name, oParameter); } } } namespace GenericsTest { class GenericMethod { public static void show<T>(T tParameter) { Console.WriteLine("This is {0}, parameter = {1}, type = {2}.", typeof(GenericMethod).Name, tParameter.GetType().Name, tParameter.ToString()); } } }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论