.NET基础之this关键字

1.限定类似名称隐藏的成员

1 public Employee(string name, string alias)
2 {
3     // Use this to qualify the fields, name and alias:
4     this.name = name;
5     this.alias = alias;
6 }

2.将对象作为参数传递给方法

1 CalcTax(this);  

3.声明索引器

静态成员函数,因为它们存在于类级别且不属于对象,不具有 this 指针。 在静态方法中引用 this 是错误的。

1 public int this[int param]
2 {
3     get { return array[param]; }
4     set { array[param] = value; }
5 }

4.实现扩展的方法

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

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

      扩展方法必须在非泛型静态类中定义。

      如果扩展方法与该类型中定义的方法具有相同的签名,则扩展方法永远不会被调用。

 1 namespace ExtensionMethods
 2 {
 3     public static class MyExtensions
 4     {
 5         public static int WordCount(this String str)
 6         {
 7             return str.Split(new char[] { ' ', '.', '?' }, 
 8                              StringSplitOptions.RemoveEmptyEntries).Length;
 9         }
10     }   
11 }

可使用 WordCount 指令将 using 扩展方法置于范围中;

1   using ExtensionMethods;  

而且,可以使用以下语法从应用程序中调用该扩展方法:

1   string s = "Hello Extension Methods";  
2   int i = s.WordCount();  

5.使用扩展方法扩展类或接口

        可以使用扩展方法来扩展类或接口,但不能重写扩展方法。 与接口或类方法具有相同名称和签名的扩展方法永远不会被调用。 编译时,扩展方法的优先级总是比类型本身中定义的实例方法低。换句话说,如果某个类型具有一个名为 Process(int i) 的方法,而你有一个具有相同签名的扩展方法,则编译器总是绑定到该实例方法。 当编译器遇到方法调用时,它首先在该类型的实例方法中寻找匹配的方法。 如果未找到任何匹配方法,编译器将搜索为该类型定义的任何扩展方法,并且绑定到它找到的第一个扩展方法。

定义接口:

1     public interface IMyInterface
2     {
3         void MethodB();
4     }

定义扩展方法:

 1     public static class ExtensionInterface
 2     {
 3         public static void MethodA(this IMyInterface myInterface,int i)
 4         {
 5             Console.WriteLine
 6                 ("Extension.MethodA(this IMyInterface myInterface, int i)");
 7         }
 8         public static void MethodA(this IMyInterface myInterface, string s)
 9         {
10             Console.WriteLine
11                 ("Extension.MethodA(this IMyInterface myInterface, string s)");
12         }
13         // This method is never called in ExtensionMethodsDemo1, because each 
14         // of the three classes A, B, and C implements a method named MethodB
15         // that has a matching signature.
16         public static void MethodB(this IMyInterface myInterface)
17         {
18             Console.WriteLine
19                 ("Extension.MethodB(this IMyInterface myInterface)");
20         }
21     }

实现接口:

 1     class A : IMyInterface
 2     {
 3         public void MethodB() { Console.WriteLine("A.MethodB()"); }
 4     }
 5 
 6     class B : IMyInterface
 7     {
 8         public void MethodB() { Console.WriteLine("B.MethodB()"); }
 9         public void MethodA(int i) { Console.WriteLine("B.MethodA(int i)"); }
10     }
11 
12     class C : IMyInterface
13     {
14         public void MethodB() { Console.WriteLine("C.MethodB()"); }
15         public void MethodA(object obj)
16         {
17             Console.WriteLine("C.MethodA(object obj)");
18         }
19     }

测试接口扩展:

 1 static void Main(string[] args)
 2 {
 3     // Declare an instance of class A, class B, and class C.
 4     A a = new A();
 5     B b = new B();
 6     C c = new C();
 7 
 8     // For a, b, and c, call the following methods:
 9     //      -- MethodA with an int argument
10     //      -- MethodA with a string argument
11     //      -- MethodB with no argument.
12 
13     // A contains no MethodA, so each call to MethodA resolves to 
14     // the extension method that has a matching signature.
15     a.MethodA(1);           // Extension.MethodA(IMyInterface, int)
16     a.MethodA("hello");     // Extension.MethodA(IMyInterface, string)
17 
18     // A has a method that matches the signature of the following call
19     // to MethodB.
20     a.MethodB();            // A.MethodB()
21 
22     // B has methods that match the signatures of the following
23     // method calls.
24     b.MethodA(1);           // B.MethodA(int)
25     b.MethodB();            // B.MethodB()
26 
27     // B has no matching method for the following call, but 
28     // class Extension does.
29     b.MethodA("hello");     // Extension.MethodA(IMyInterface, string)
30 
31     // C contains an instance method that matches each of the following
32     // method calls.
33     c.MethodA(1);           // C.MethodA(object)
34     c.MethodA("hello");     // C.MethodA(object)
35     c.MethodB();            // C.MethodB()
36 
37     Console.ReadLine();
38 }

6.转自

https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/classes-and-structs/extension-methods

https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/this

posted @ 2018-04-10 21:31  imstrive  阅读(1055)  评论(0编辑  收藏  举报