• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#泛型(一)泛型方法

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
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 } }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#DataGridView中合并单元格发布时间:2022-07-13
下一篇:
C#委托-多播委托调用多个方法发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap