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

 public Employee(string name, string alias)
{
// Use this to qualify the fields, name and alias:
this.name = name;
this.alias = alias;
}

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

 CalcTax(this);  

3.声明索引器

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

 public int this[int param]
{
get { return array[param]; }
set { array[param] = value; }
}

4.实现扩展的方法

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

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

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

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

 namespace ExtensionMethods
{
public static class MyExtensions
{
public static int WordCount(this String str)
{
return str.Split(new char[] { ' ', '.', '?' },
StringSplitOptions.RemoveEmptyEntries).Length;
}
}
}

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

   using ExtensionMethods;  

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

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

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

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

定义接口:

     public interface IMyInterface
{
void MethodB();
}

定义扩展方法:

     public static class ExtensionInterface
{
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)");
}
// This method is never called in ExtensionMethodsDemo1, because each
// of the three classes A, B, and C implements a method named MethodB
// that has a matching signature.
public static void MethodB(this IMyInterface myInterface)
{
Console.WriteLine
("Extension.MethodB(this IMyInterface myInterface)");
}
}

实现接口:

     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)");
}
}

测试接口扩展:

 static void Main(string[] args)
{
// Declare an instance of class A, class B, and class C.
A a = new A();
B b = new B();
C c = new C(); // For a, b, and c, call the following methods:
// -- MethodA with an int argument
// -- MethodA with a string argument
// -- MethodB with no argument. // A contains no MethodA, so each call to MethodA resolves to
// the extension method that has a matching signature.
a.MethodA(); // Extension.MethodA(IMyInterface, int)
a.MethodA("hello"); // Extension.MethodA(IMyInterface, string) // A has a method that matches the signature of the following call
// to MethodB.
a.MethodB(); // A.MethodB() // B has methods that match the signatures of the following
// method calls.
b.MethodA(); // B.MethodA(int)
b.MethodB(); // B.MethodB() // B has no matching method for the following call, but
// class Extension does.
b.MethodA("hello"); // Extension.MethodA(IMyInterface, string) // C contains an instance method that matches each of the following
// method calls.
c.MethodA(); // C.MethodA(object)
c.MethodA("hello"); // C.MethodA(object)
c.MethodB(); // C.MethodB() Console.ReadLine();
}

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

随机推荐

  1. MySql避免重复插入记录

    今天用python抓取数据入库需要避免重复数据插入,在网上找了一些方法: 方案一:使用ignore关键字 如果是用主键primary或者唯一索引unique区分了记录的唯一性,避免重复插入记录可以使用 ...

  2. android自定义控件(5)-实现ViewPager效果

    对于系统的ViewGroup我们已经是十分熟悉了,最常用的LinearLayout和RelativeLayout几乎是天天要打交道,下面我们就来看看,如何一步一步将其实现: 一.首先当然也是最通常的新 ...

  3. CFgym Board Queries (旋转、翻转简化)

    http://codeforces.com/gym/100497 codeforces 2014-2015 CT S02E04: Codeforces Trainings Season 2 Episo ...

  4. django 文件上传

    模板文件: <form method='post' action='/script/upload/' enctype="multipart/form-data" accept ...

  5. nyoj 236拦截导弹 简单动归(java)

    C/C++: #include<stdio.h> int main() { // freopen("250.txt","r",stdin); ],b ...

  6. Marsedit 破解版下载(3.5.6)

    Marsedit 破解版下载(3.5.6) 最近在 Mac 端写文章经常遇到棘手的问题,最近发现了 marsedit 现在分享给大家 marsedit downloads

  7. PHP如何释放内存之unset销毁变量并释放内存详解

    PHP的unset()函数用来清除.销毁变量,不用的变量,我们可以用unset()将它销毁.但是某些时候,用unset()却无法达到销毁变量占用的内存!我们先看一个例子: <?php $s = ...

  8. Mac安装OpenCV

    安装过程参考这篇文章Mac平台上OpenCV开发环境搭建 也可以参考文档官网上的安装文档Installation in Linux(不知道为什么没有Installation in Mac...) 我的 ...

  9. Windows下几个常用的和进程有关的命令

    在windows下,进入cmd,有几个常用的和进程有关的命令: netstat -ano:查看所有进程 netstat -ano|findstr  “端口号”:查看端口号对应的进程PID taskli ...

  10. NFS工作原理及配置文件详解

    nfs工作原理流程       如上图所示,当访问程序通过NFS客户端向NFS服务端存取文件时,其请求数据流程如下几点:     1.首先用户访问网站程序,由程序在NFS客户端上发出NFS文件存取功能 ...