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

.NET基础之this关键字的更多相关文章

  1. Java SE 基础:常用关键字

    Java SE 基础:常用关键字 常用关键字表

  2. Java之--Java语言基础组成(关键字、标识符、注释、常量和变量、运算符)

    Java语言基础组成-关键字.标识符.注释.常量和变量.运算符 Java语言由8个模块构成,分别为:1.关键字:2.标识符(包名.类名.接口名.常量名.变量名等):3.注释:4.常量和变量:5.运算符 ...

  3. JAVA_SE基础——6.标识符&关键字

    学会写helloworld之后,  我们就开始来认识标识符&关键字 一.标识符 标识符是指可被用来为类.变量或方法等命名的字符序列,换言之,标识符就是用户自定义的名称来标识类.变量或方法等.更 ...

  4. Java基础-标识符与关键字

    Java基础-标识符与关键字 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.什么是标识符 标识符就是程序员在编写程序时,给类,变量,方法等起的名字. 二.标识符的命名规则 1& ...

  5. java学习第02天(语言基础组成:关键字、标识符、注释、常量和变量)

    Java语言基础组成 1. 关键字 就是指的一些单词,这些单词被赋予了特殊的java含义,就不再叫单词了. 例如: class Demo{ public static void main(String ...

  6. Java编程基础——标识符和关键字

    Java编程基础——标识符和关键字 摘要:本文主要介绍标识符和关键字. 标识符 是什么 Java语言中,为各种变量.方法.类和包等起的名字,统统称之为Java标识符. 命名规则 ◆ 应以字母.下划线. ...

  7. Java基础语法(1)-关键字与保留字

    title: Java基础语法(1)-关键字与保留字 blog: CSDN data: Java学习路线及视频 1.关键字 关键字(keyword)的定义和特点 定义:被Java语言赋予了特殊含义,用 ...

  8. java多线程基础(synchronize关键字)

    [toc] 基础知识 ---- 线程:进程(process)就是一块包含了某些资源的内存区域.操作系统利用进程把它的工作划分为一些功能单元. 线程:进程中所包含的一个或多个执行单元称为线程(threa ...

  9. Java基础系列--static关键字

    原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/8477914.html 一.概述 static关键字是Java诸多关键字中较常使用的一个,从 ...

  10. -1-1 java 基础语法 java关键字 java 注释 常量 语句 运算符 函数 数组定义

    Java语言基础组成 关键字 标识符 注释 常量和变量 运算符 语句 函数 数组 关键字 定义:被Java语言赋予了特殊含义的单词 特点:关键字中所有字母都为小写 用于定义数据类型的关键字 class ...

随机推荐

  1. Storm基本原理概念及基本使用

    1. 背景介绍 1.1 离线计算是什么 离线计算:批量获取数据.批量传输数据.周期性批量计算数据.数据展示: 代表技术:Sqoop批量导入数据.HDFS批量存储数据.MapReduce批量计算数据.H ...

  2. nginx从http跳转到https

    场景 项目前期使用http,后期为了安全方面的考虑,启用了https. 项目架构:前端使用nginx作为多个tomcat实例的反向代理和负载均衡. 实际上只需要在nginx上启用https即可,使客户 ...

  3. Win32程序框架

    // WinMsg.cpp : 定义应用程序的入口点. // #include "stdafx.h" #include <stdio.h> #include " ...

  4. springboot(十四):springboot整合mybatis

    org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'multipart/form-data;bounda ...

  5. 【python小练】0004

    第 0004 题:任一个英文的纯文本文件,统计其中的单词出现的个数. 先回忆一下各种括号的用途: () tuple [] list {} dict ([]) set——需要一个list作为输入合集 c ...

  6. [C++]Linux之读取计算机网络数据[/proc/net/dev]

    #include<stdlib.h> #include<stdio.h> #define BUFFER_SIZE 256 int main(){ FILE *stream; c ...

  7. cpp 标准库

    源:http://bbs.csdn.net/topics/300040713 C++标准库的所有头文件都没有扩展名.C++标准库的内容总共在50个标准头文件中定义,其中18个提供了C库的功能.< ...

  8. Coursera, Machine Learning, notes

      Basic theory (i) Supervised learning (parametric/non-parametric algorithms, support vector machine ...

  9. python 的基础 学习 11天 作业题

    1.整理函数相关知识点,写博客 2.写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者. def func1(argv): li = [] for i in r ...

  10. border-radius图解

    自己看了理解的border-radius: 设置参数: 100*100的正方形,第一个:border-top-left-radius:100px 100px,即圆的半径为100px.圆弧与上和左bor ...