C# this关键字引用类的当前实例。

成都创新互联公司自2013年创立以来,是专业互联网技术服务公司,拥有项目成都网站制作、网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元鄱阳做网站,已为上家服务,为鄱阳各地企业和个人服务,联系电话:18980820575
以下是 this 的常用用途:
◆限定被相似的名称隐藏的成员
◆将对象作为参数传递到其他方法
◆声明索引器
C# this关键字示例:
- //this关键字
 - //keywords_this.cs
 - usingSystem;
 - classEmployee
 - {
 - privatestring_name;
 - privateint_age;
 - privatestring[]_arr=newstring[5];
 - publicEmployee(stringname,intage)
 - {
 - //使用this限定字段,name与age
 - this._name=name;
 - this._age=age;
 - }
 - publicstringName
 - {
 - get{returnthis._name;}
 - }
 - publicintAge
 - {
 - get{returnthis._age;}
 - }
 - //打印雇员资料
 - publicvoidPrintEmployee()
 - {
 - //将Employee对象作为参数传递到DoPrint方法
 - Print.DoPrint(this);
 - }
 - //声明索引器
 - publicstringthis[intparam]
 - {
 - get{return_arr[param];}
 - set{_arr[param]=value;}
 - }
 - }
 - classPrint
 - {
 - publicstaticvoidDoPrint(Employeee)
 - {
 - Console.WriteLine("Name:{0}\nAge:{1}",e.Name,e.Age);
 - }
 - }
 - classTestApp
 - {
 - staticvoidMain()
 - {
 - EmployeeE=newEmployee("Hunts",21);
 - E[0]="Scott";
 - E[1]="Leigh";
 - E[4]="Kiwis";
 - E.PrintEmployee();
 - for(inti=0;i<5;i++)
 - {
 - Console.WriteLine("FriendsName:{0}",E[i]);
 - }
 - Console.ReadLine();
 - }
 - }
 - /**//*
 - 控制台输出:
 - Name:Hunts
 - Age:21
 - FriendsName:Scott
 - FriendsName:Leigh
 - FriendsName:
 - FriendsName:
 - FriendsName:Kiwis
 - */
 
由于静态成员函数存在于类一级,并且不是对象的一部分,因此没有this指针。在静态方法中引用C# this关键字是错误的。索引器允许类或结构的实例按照与数组相同的方式进行索引。索引器类似于属性,不同之处在于它们的访问器采用参数。