如有错误,欢迎指正。

1.代表当前类,在当前类中可使用this访问当前类成员变量和方法(需要注意的是 静态方法中不能使用this),也可用于参数传递,传递当前对象的引用。

下面贴代码:

    class Program
{
static void Main(string[] args)
{
thisClass testObj = new thisClass();
Console.ReadLine();
}
} class thisClass
{
private string A { get; set; }
public thisClass()
{
/*当前类this 访问类中属性A 静态方法无法访问A属性*/
this.A = "Test String";
Console.WriteLine(this.TestFun("TestFun :"));
}
private string TestFun(string args)
{
return args + this.A;
}
}

2.声明索引器

索引器:允许类和结构的实例按照与数组相同的方式进行索引,索引器类似与属性,不同之处在于他们的访问器采用参数,被称为有参属性,索引可以被重载,属于实例成员,不能声明为static。

下面贴代码:

    class Program
{
static void Main(string[] args)
{
indexClass intIndexClass = new indexClass();
intIndexClass[0] = new thisClass("intIndexClass 111");
intIndexClass[1] = new thisClass("intIndexClass 222");
indexClass stringIndexClass = new indexClass();
stringIndexClass["string1"] = new thisClass("stringIndexClass string1");
stringIndexClass["string2"] = new thisClass("stringIndexClass string2");
Console.ReadLine();
}
} class indexClass
{
/*声明属性*/
private thisClass[] thisClassArr = new thisClass[10];
private Hashtable thisClassStrArr = new Hashtable();
/*创建索引器1 索引可以被重载,属于实例成员,不能声明为static*/
public thisClass this[int index]
{
get { return thisClassArr[index]; }
set { this.thisClassArr[index] = value; }
}
/*创建索引器2*/
public thisClass this[string index]
{
get
{
return thisClassStrArr[index] as thisClass;
}
set { this.thisClassStrArr[index] = value; }
}
} class thisClass
{
private string A { get; set; }
public thisClass(string str)
{
/*当前类this 访问类中属性A 静态方法无法访问A属性*/
this.A = str;
Console.WriteLine(this.TestFun("TestFun :"));
}
private string TestFun(string args)
{
return args + this.A;
}
}

3.用于扩展方法

扩展方法的要素: 
1.此方法必须是一个静态方法 
2.此方法必须放在静态类中 
3.此方法的第一个参数必须以this开头,并且指定此方法是扩展自哪个类型

public static string DateToString(this DateTime dt)
{
return dt.ToString("yyyy-mm-dd hh:mm:ss");
} static void Main(string[] args)
{
DateTime now = DateTime.Now;
string time = now.DateToString();
Console.WriteLine(time);
Console.ReadKey();
}

我看了好像就这么多,其他还有补充的没?

C#学习系列-this的使用的更多相关文章

  1. 分布式学习系列【dubbo入门实践】

    分布式学习系列[dubbo入门实践] dubbo架构 组成部分:provider,consumer,registry,monitor: provider,consumer注册,订阅类似于消息队列的注册 ...

  2. Entity Framework Code First学习系列目录

    Entity Framework Code First学习系列说明:开发环境为Visual Studio 2010 + Entity Framework 5.0+MS SQL Server 2012, ...

  3. WCF学习系列汇总

    最近在学习WCF,打算把一整个系列的文章都”写“出来,包括理论和实践,这里的“写”是翻译,是国外的大牛写好的,我只是搬运工外加翻译.翻译的不好,大家请指正,谢谢了.如果觉得不错的话,也可以给我点赞,这 ...

  4. EF(Entity Framework)系统学习系列

    好久没写博客了,继续开启霸屏模式,好了,废话不多说,这次准备重新系统学一下EF,一个偶然的机会找到了一个学习EF的网站(http://www.entityframeworktutorial.net/) ...

  5. MVC学习系列4--@helper辅助方法和用户自定义HTML方法

    在HTML Helper,帮助类的帮助下,我们可以动态的创建HTML控件.HTML帮助类是在视图中,用来呈现HTML内容的.HTML帮助类是一个方法,它返回的是string类型的值. HTML帮助类, ...

  6. YYKit学习系列 ---- 开篇

    准备花半年时间系统学习YYKit,  学习过程会放入"YYKit学习系列"这个分类, 喜欢YYKit的可以随时留意我的文章, 一起学习!!!

  7. RabbitMQ学习系列(四): 几种Exchange 模式

    上一篇,讲了RabbitMQ的具体用法,可以看看这篇文章:RabbitMQ学习系列(三): C# 如何使用 RabbitMQ.今天说些理论的东西,Exchange 的几种模式. AMQP协议中的核心思 ...

  8. RabbitMQ学习系列(三): C# 如何使用 RabbitMQ

    上一篇已经讲了Rabbitmq如何在Windows平台安装,还不了解如何安装的朋友,请看我前面几篇文章:RabbitMQ学习系列一:windows下安装RabbitMQ服务 , 今天就来聊聊 C# 实 ...

  9. [翻译svg教程]svg学习系列 开篇

    目录 [翻译svg教程]svg学习系列 开篇 [翻译svg教程 ]svg 的坐标系统 [翻译svg教程]svg 中的g元素 [翻译svg教程]svg中矩形元素 rect [翻译svg教程]svg中的c ...

  10. C#学习系列-文章导航

    C#学习系列-.NET体系结构 C#学习系列-类与结构的区别 C#学习系列-String与string的区别 C#学习系列-抽象方法与虚拟方法的区别 C#学习系列-out与ref的区别 C#学习系列- ...

随机推荐

  1. C# Exception 写入文件

    /// <summary> /// 将异常打印到LOG文件 /// </summary> /// <param name="ex">异常< ...

  2. 从up6-down2升级到down3

    概述: 添加存储过程down_f_process.sql,down_f_del.sql 更新DnFile.updateProcess,DnFile.Delete 更新down.js 更新down.fo ...

  3. [Leetcode][JAVA] Interleaving String

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

  4. 基于Axure的快速原型方法

    Axure是一个专业的快速原型设计工具,让负责定义需求和规格.设计功能和界面的专家能够快速创建应用软件或Web网站的线框图.流程图.原型和规格说明文档.作为专业的原型设计工具,它能快速.高效的创建原型 ...

  5. TreeMap源码分析

    MapClassDiagram

  6. 项目修改有感_主要是以js、Gridview为主

    1.弹出提示:confirm--弹出的窗口有确认.取消按钮 alert--弹出的窗口只有确认按钮 例:若需要在点击确认后执行其他操作(confirm) var toAlert = confirm(&q ...

  7. Hibernate component mapping

    A Component is a containted object that is be persisted value type and not an entity.But you can emb ...

  8. STM32串口接收不定长数据原理与源程序(转)

    今天说一下STM32单片机的接收不定长度字节数据的方法.由于STM32单片机带IDLE中断,所以利用这个中断,可以接收不定长字节的数据,由于STM32属于ARM单片机,所以这篇文章的方法也适合其他的A ...

  9. 无法将类型为“System.Decimal”的对象强制转换为类型“System.Char[]”。

    在用微软的SSIS操作ORACLE 数据源的时候碰到以下报错信息: [ADO NET Destination [13455]] 错误: 数据插入期间出现异常,从提供程序返回的消息为:无法将类型为&qu ...

  10. 从KRE到XRE:ASP.NET 5中正在消失的那些K

    前几天写了篇博客ASP.NET 5中的那些K,刚把ASP.NET 5中的那些K搞明白了些,昨天发现微软正在让那些K消失. 首先是在 KRuntime 的git日志中发现的: * Runtime ren ...