• .NET 扩展方法


     

    • 扩展方法
         扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型、重新编译或以其他方式修改原始类型。 扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用。 对于用 C# 和 Visual Basic 编写的客户端代码,调用扩展方法与调用在类型中实际定义的方法之间没有明显的差异。
         通常,建议您只在不得已的情况下才实现扩展方法,并谨慎地实现。 只要有可能,必须扩展现有类型的客户端代码都应该通过创建从现有类型派生的新类型来达到这一目的。
    • 限制条件
      • 必须定义在静态类中
      • 必须使用this关键字对第一个参数进行修饰
      • 每个扩展方法只能被内存中正确的实例和其所在的静态类调用
    • 代码示例
    namespace DefineIMyInterface
    {
        using System;
        public interface IMyInterface
        {
            void MethodB();
        }
    }
    namespace Extensions
    {
        using System;
        using DefineIMyInterface;
        public static class Extension
        {
            public static void MethodA(this IMyInterface myInterface, int i)
            {
                Console.WriteLine
                    ("Extension.MethodA(this IMyInterface myInterface, int i)");
            }
            public static void MethodA(this IMyInterface myInterface, string s)
            {
                Console.WriteLine
                    ("Extension.MethodA(this IMyInterface myInterface, string s)");
            }
            public static void MethodB(this IMyInterface myInterface)
            {
                Console.WriteLine
                    ("Extension.MethodB(this IMyInterface myInterface)");
            }
        }
    }
    namespace ExtensionMethodsDemo1
    {
        using System;
        using Extensions;
        using DefineIMyInterface;
        class A : IMyInterface
        {
            public void MethodB() { Console.WriteLine("A.MethodB()"); }
        }
        class B : IMyInterface
        {
            public void MethodB() { Console.WriteLine("B.MethodB()"); }
            public void MethodA(int i) { Console.WriteLine("B.MethodA(int i)"); }
        }
        class C : IMyInterface
        {
            public void MethodB() { Console.WriteLine("C.MethodB()"); }
            public void MethodA(object obj)
            {
                Console.WriteLine("C.MethodA(object obj)");
            }
        }
        class ExtMethodDemo
        {
            static void Main(string[] args)
            {
                A a = new A();
                B b = new B();
                C c = new C();
                a.MethodA(1);           // Extension.MethodA(object, int)
                a.MethodA("hello");     // Extension.MethodA(object, string)
                a.MethodB();            // A.MethodB()
                b.MethodA(1);           // B.MethodA(int)
                b.MethodB();            // B.MethodB()
                b.MethodA("hello");     // Extension.MethodA(object, string)
                c.MethodA(1);           // C.MethodA(object)
                c.MethodA("hello");     // C.MethodA(object)
                c.MethodB();            // C.MethodB()
            }
        }
    }
    
  • 相关阅读:
    poj 3528 (三维几何求凸包+凸包表面积)
    dijkstra模板(好像是斐波那契额堆优化,但我为什么看起来像优先队列优化,和spfa一样)
    最大空凸包模板
    ICPC 2017–2018, NEERC, Northern Subregional Contest St Petersburg, November 4, 2017 I题
    hdu 5248 序列变换
    hdu 2063(二分图模板测试)
    组合数
    85. Maximal Rectangle 由1拼出的最大矩形
    750. Number Of Corner Rectangles四周是点的矩形个数
    801. Minimum Swaps To Make Sequences Increasing 为使两个数组严格递增,所需要的最小交换次数
  • 原文地址:https://www.cnblogs.com/liusuqi/p/3125553.html
Copyright © 2020-2023  润新知