• 扩展方法略好于帮助方法


    如果针对一个类型实例的代码片段经常被用到,我们可能会想到把之封装成帮助方法。如下是一段针对DateTime类型实例的一段代码:

        class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                DateTime d = new DateTime(2001,5,18);
    
                switch (d.DayOfWeek)
    
                {
    
                    case DayOfWeek.Saturday:
    
                    case DayOfWeek.Sunday:
    
                        DoWeekendThing();
    
                        break;
    
                    default:
    
                        DoWeekDayThing();
    
                        break;
    
                }
    
                Console.ReadKey();
    
            }
    
            static void DoWeekendThing()
    
            {
    
                Console.WriteLine("周末好好休息放松心情");
    
            }
    
            static void DoWeekDayThing()
    
            {
    
                Console.WriteLine("认真工作");
    
            }
    
        }
    

    以上,把判断是否是周末的代码片段封装到帮助类、帮助方法中如下:

        public static class DateTimeHelper
    
        {
    
            public static bool IsWeekend(DateTime dateTime)
    
            {
    
                switch (dateTime.DayOfWeek)
    
                {
    
                    case DayOfWeek.Saturday:
    
                    case DayOfWeek.Sunday:
    
                        return true;
    
                    default:
    
                        return false;
    
                }
    
            }
    
        }

     

    在客户端:

     

        class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                DateTime d = new DateTime(2001,5,18);
    
                if (DateTimeHelper.IsWeekend(d))
    
                {
    
                    DoWeekendThing();
    
                }
    
                else
    
                {
    
                    DoWeekDayThing();
    
                }
    
                Console.ReadKey();
    
            }
    
            static void DoWeekendThing()
    
            {
    
                Console.WriteLine("周末好好休息放松心情");
    
            }
    
            static void DoWeekDayThing()
    
            {
    
                Console.WriteLine("认真工作");
    
            }
    
        }
    

     

    用帮助类、帮助方法固然好,因为进行了很好的封装,但每次都必须要记住扩展方法在DateTimeHelper这个扩展类中。如果System.DateTime包括IsWeekend方法会更好!

     

    扩展方法是一种特殊的静态方法,可以让编写的方法像现有类型的实例方法一样被使用。

       public static class DateTimeExtensions
    
        {
    
            public static bool IsWeekend(this DateTime dateTime)
    
            {
    
                switch (dateTime.DayOfWeek)
    
                {
    
                    case DayOfWeek.Saturday:
    
                    case DayOfWeek.Sunday:
    
                        return true;
    
                    default:
    
                        return false;   
    
                }
    
            }
    
        }

    ○ 扩展方法必须在静态类中
    ○ 扩展方法必须是静态方法
    ○ 扩展方法的第一个参数必须是需要被扩展的类型,而且前面必须加
    this关键字  

     

    客户端修改为:

        class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                DateTime d = new DateTime(2001,5,18);
    
                if (d.IsWeekend())
    
                {
    
                    DoWeekendThing();
    
                }
    
                else
    
                {
    
                    DoWeekDayThing();
    
                }
    
                Console.ReadKey();
    
            }
    
            static void DoWeekendThing()
    
            {
    
                Console.WriteLine("周末好好休息放松心情");
    
            }
    
            static void DoWeekDayThing()
    
            {
    
                Console.WriteLine("认真工作");
    
            }
    
        }
    

    总结:扩展方法貌似略好于帮助方法,可以作为类型实例的方法直接被调用。

  • 相关阅读:
    Linux下配置APACHE支持PHP环境
    mysql 管理脚本
    RAC迁移至单机考虑几大因素
    mysql配置文件my.cnf模板
    hadoop 日常问题汇总(持续更新)
    Redis配置文件
    jquery操作select(增加,删除,清空)
    mybatis异常
    Elasticsearch 之 query与filter区别
    在elasticsearch里如何高效的使用filter
  • 原文地址:https://www.cnblogs.com/darrenji/p/4114699.html
Copyright © 2020-2023  润新知