新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#this關(guān)鍵字介紹
C# this關(guān)鍵字引用類的當(dāng)前實例。

成都創(chuàng)新互聯(lián)公司自2013年創(chuàng)立以來,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都網(wǎng)站制作、網(wǎng)站建設(shè)網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元鄱陽做網(wǎng)站,已為上家服務(wù),為鄱陽各地企業(yè)和個人服務(wù),聯(lián)系電話:18980820575
以下是 this 的常用用途:
◆限定被相似的名稱隱藏的成員
◆將對象作為參數(shù)傳遞到其他方法
◆聲明索引器
C# this關(guān)鍵字示例:
- //this關(guān)鍵字
- //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對象作為參數(shù)傳遞到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
- */
由于靜態(tài)成員函數(shù)存在于類一級,并且不是對象的一部分,因此沒有this指針。在靜態(tài)方法中引用C# this關(guān)鍵字是錯誤的。索引器允許類或結(jié)構(gòu)的實例按照與數(shù)組相同的方式進行索引。索引器類似于屬性,不同之處在于它們的訪問器采用參數(shù)。
標(biāo)題名稱:C#this關(guān)鍵字介紹
文章出自:http://m.fisionsoft.com.cn/article/dhjhhgj.html


咨詢
建站咨詢
