• C#泛型(一)泛型方法


    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());
    
            }
        }
    }
    

    namespace GenericsTest { class MonitorMethod { public static void Show() { int iValue = 12345; long commonSecond = 0; long objectSecond = 0; long genericSecond = 0; { Stopwatch watch = new Stopwatch(); watch.Start(); for (int i = 0; i < 100000000; i++) { ShowInt(iValue); } watch.Stop(); commonSecond = watch.ElapsedMilliseconds; } { Stopwatch watch = new Stopwatch(); watch.Start(); for (int i = 0; i < 100000000; i++) { ShowObject(iValue); } watch.Stop(); objectSecond = watch.ElapsedMilliseconds; } { Stopwatch watch = new Stopwatch(); watch.Start(); for (int i = 0; i < 100000000; i++) { Show<int>(iValue); } watch.Stop(); genericSecond = watch.ElapsedMilliseconds; } Console.WriteLine("commonSecond={0},objectSecond={1},genericSecond={2}." , commonSecond, objectSecond, genericSecond); } #region PrivateMethod private static void ShowInt(int iParameter) { //do nothing } private static void ShowObject(object oParameter) { //do nothing } private static void Show<T>(T tParameter) { //do nothing } #endregion } }
  • 相关阅读:
    微信 JS SDK 的 chooseImage 接口在部分安卓机上容易造成页面刷新
    规约模式Specification Pattern
    ASP.NET Core 1.0基础之日志
    C# 7 新特性-2
    C# 7 新特性-1
    ASP.NET Core 1.0基础之诊断
    ASP.NET Core 1.0基础之依赖注入
    ASP.NET Core 1.0 基础之配置
    ASP.NET Core 1.0基础之静态文件处理
    FreeSql生产环境自动升级数据库解决方案
  • 原文地址:https://www.cnblogs.com/wsq-blog/p/10456919.html
Copyright © 2020-2023  润新知