zoukankan      html  css  js  c++  java
  • C#原始类型扩展方法—this参数修饰符

     

     
          扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型、重新编译或以其他方式修改原始类型。扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用。对于用 C# 和 Visual Basic 编写的客户端代码,调用扩展方法与调用在类型中实际定义的方法之间没有明显的差异。

      

          扩展方法被定义为静态方法,但它们是通过实例方法语法进行调用的。它们的第一个参数指定该方法作用于哪个类型,并且该参数以 this 修饰符为前缀。仅当您使用 using 指令将命名空间显式导入到源代码中之后,扩展方法才位于范围中。

      下面的示例演示为 System.String 类定义的一个扩展方法。请注意,它是在非嵌套、非泛型静态类内部定义的:
         namespace ExtensionMethods
         {
             public static class MyExtensions
             {
                 public static int WordCount(this String str)
                 {
                     return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
                 }
             }   
         }


      可使用以下 using 指令将 WordCount 扩展方法放入范围中:
      using ExtensionMethods;
      而且,可以在应用程序中使用以下语法对该扩展方法进行调用:
      string s = "Hello Extension Methods";
      int i = s.WordCount();

      在代码中,可以使用实例方法语法调用该扩展方法。但是,编译器生成的中间语言 (IL) 会将代码转换为对静态方法的调用。因此,并未真正违反封装原则。实际上,扩展方法无法访问它们所扩展的类型中的私有变量。

    定义和调用扩展方法:
     1、定义一个静态类以包含扩展方法。该类必须对客户端代码可见。有关可访问性规则的更多信息,请参见访问修饰符(C# 编程指南)。
     2、将该扩展方法实现为静态方法,并使其至少具有与包含类相同的可见性。
     3、该方法的第一个参数指定方法所操作的类型;该参数必须以 this 修饰符开头。
     4、在调用代码中,添加一条 using 指令以指定包含扩展方法类的命名空间。
     5、按照与调用类型上的实例方法一样的方式调用扩展方法。
      请注意,第一个参数不是由调用代码指定的,因为它表示正应用运算符的类型,并且编译器已经知道对象的类型。您只需通过 n 为这两个形参提供实参。

    示例
      下面的示例在 MyExtensions.StringExtension 类中实现了一个名为 WordCount 的扩展方法。该方法对 String 类进行操作,而该类被指定为第一个方法参数。MyExtensions 命名空间被导入到应用程序命名空间中,并且该方法是在 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);
       }
    }
    }

  • 相关阅读:
    Beyond Compare设置
    lt29配置
    [Windows] 常用的快捷命令
    【C&C++】数组初始化的思考
    【Windows批处理】常用命令解析
    Linux下将网站数据自动备份到Dropbox
    CentOS下iRedMail安装配置
    Python 大数据思维导图
    libxml2编程基础
    xml文件的解析
  • 原文地址:https://www.cnblogs.com/lvdongjie/p/5559771.html
Copyright © 2011-2022 走看看