zoukankan      html  css  js  c++  java
  • 扩展方法 C#

    扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型、重新编译或以其他方式修改原始类型。

    定义和调用扩展方法

    1. 定义一个静态以包含扩展方法。

      该类必须对客户端代码可见。 有关可访问性规则的更多信息,请参见访问修饰符

    2. 将该扩展方法实现为静态方法,并使其至少具有与包含类相同的可见性。

    3. 该方法的第一个参数指定方法所操作的类型;该参数必须以 this 修饰符开头。

    4. 在调用代码中,添加一条 using 指令以指定包含扩展方法类的命名空间

    5. 按照与调用类型上的实例方法一样的方式调用扩展方法。

      请注意,第一个参数不是由调用代码指定的,因为它表示正应用运算符的类型,并且编译器已经知道对象的类型。 您只需通过 n 为这两个形参提供实参。

    示例
     
     
     

    下面的示例在 CustomExtensions.StringExtension 类中实现了一个名为 WordCount 的扩展方法。 该方法对 String 类进行操作,而该类被指定为第一个方法参数。 CustomExtensions 命名空间被导入到应用程序命名空间中,并且该方法是在 Main 方法内调用的。

    using System.Linq;
    using System.Text;
    using System;
    
    namespace CustomExtensions
    {
        //Extension methods must be defined in a static class
        public static class StringExtension
        {
            // This is the extension method.
            // The first parameter takes the "this" modifier
            // and specifies the type for which the method is defined.
            public static int WordCount(this String str)
            {
                return str.Split(new char[] {' ', '.','?'}, StringSplitOptions.RemoveEmptyEntries).Length;
            }
        }
    }
    namespace Extension_Methods_Simple
    {
        //Import the extension method namespace.
        using CustomExtensions;
        class Program
        {
            static void Main(string[] args)
            {
                string s = "The quick brown fox jumped over the lazy dog.";
                //  Call the method as if it were an 
                //  instance method on the type. Note that the first
                //  parameter is not specified by the calling code.
                int i = s.WordCount();
                System.Console.WriteLine("Word count of s is {0}", i);
            }
        }
    }
  • 相关阅读:
    java中synchronized关键字分析
    如何查看java的class文件
    安装jdk1.9后报 Error:java: 无效的源发行版: 1.9
    mybatis拦截器获取sql
    NOIP2015 T4 推销员 贪心+堆优化
    BZOJ 2761: [JLOI2011]不重复数字 hash哈希
    Contest-hunter 暑假送温暖 SRM01
    Contest-hunter 暑假送温暖 SRM08
    洛谷P3393逃离僵尸岛 最短路
    bzoj 1433: [ZJOI2009]假期的宿舍 最大流
  • 原文地址:https://www.cnblogs.com/dayang12525/p/6281744.html
Copyright © 2011-2022 走看看