自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C# this關(guān)鍵字介紹

開發(fā) 后端
這里介紹C# this關(guān)鍵字,由于靜態(tài)成員函數(shù)存在于類一級,并且不是對象的一部分,因此沒有this指針。在靜態(tài)方法中引用C# this關(guān)鍵字是錯(cuò)誤的。

C# this關(guān)鍵字引用類的當(dāng)前實(shí)例。

以下是 this 的常用用途:
◆限定被相似的名稱隱藏的成員
◆將對象作為參數(shù)傳遞到其他方法
◆聲明索引器

C# this關(guān)鍵字示例:

  1. //this關(guān)鍵字  
  2. //keywords_this.cs  
  3. usingSystem;  
  4. classEmployee  
  5. {  
  6. privatestring_name;  
  7. privateint_age;  
  8. privatestring[]_arr=newstring[5];  
  9.  
  10. publicEmployee(stringname,intage)  
  11. {  
  12. //使用this限定字段,name與age  
  13. this._name=name;  
  14. this._age=age;  
  15. }  
  16.  
  17. publicstringName  
  18. {  
  19. get{returnthis._name;}  
  20. }  
  21.  
  22. publicintAge  
  23. {  
  24. get{returnthis._age;}  
  25. }  
  26.  
  27. //打印雇員資料  
  28. publicvoidPrintEmployee()  
  29. {  
  30. //將Employee對象作為參數(shù)傳遞到DoPrint方法  
  31. Print.DoPrint(this);  
  32. }  
  33.  
  34. //聲明索引器  
  35. publicstringthis[intparam]  
  36. {  
  37. get{return_arr[param];}  
  38. set{_arr[param]=value;}  
  39. }  
  40.  
  41. }  
  42. classPrint  
  43. {  
  44. publicstaticvoidDoPrint(Employeee)  
  45. {  
  46. Console.WriteLine("Name:{0}\nAge:{1}",e.Name,e.Age);  
  47. }  
  48. }  
  49.  
  50. classTestApp  
  51. {  
  52. staticvoidMain()  
  53. {  
  54. EmployeeE=newEmployee("Hunts",21);  
  55. E[0]="Scott";  
  56. E[1]="Leigh";  
  57. E[4]="Kiwis";  
  58. E.PrintEmployee();  
  59.  
  60. for(inti=0;i<5;i++)  
  61. {  
  62. Console.WriteLine("FriendsName:{0}",E[i]);  
  63. }  
  64.  
  65. Console.ReadLine();  
  66. }  
  67. }  
  68.  
  69. /**//*  
  70. 控制臺輸出:  
  71. Name:Hunts  
  72. Age:21  
  73. FriendsName:Scott  
  74. FriendsName:Leigh  
  75. FriendsName:  
  76. FriendsName:  
  77. FriendsName:Kiwis  
  78. */ 

由于靜態(tài)成員函數(shù)存在于類一級,并且不是對象的一部分,因此沒有this指針。在靜態(tài)方法中引用C# this關(guān)鍵字是錯(cuò)誤的。索引器允許類或結(jié)構(gòu)的實(shí)例按照與數(shù)組相同的方式進(jìn)行索引。索引器類似于屬性,不同之處在于它們的訪問器采用參數(shù)。

【編輯推薦】

  1. 如何用C#和ADO.NET訪問
  2. 淺析C# Switch語句
  3. C#驗(yàn)證輸入方法詳解
  4. 簡單介紹C# 匿名方法
  5. C# FileSystemWatcher對象
責(zé)任編輯:佚名 來源: 博客園
相關(guān)推薦

2009-09-02 09:24:03

C# this關(guān)鍵字

2009-08-06 17:52:23

C#增加that關(guān)鍵字

2009-08-13 17:44:34

C# using關(guān)鍵字

2009-08-21 14:47:59

C# base關(guān)鍵字

2009-08-26 15:16:29

C# lock關(guān)鍵字

2009-08-13 13:04:29

C# lock關(guān)鍵字

2009-08-21 14:16:35

C# New關(guān)鍵字

2009-08-13 09:49:16

C#關(guān)鍵字

2011-06-21 09:50:51

volatile

2009-07-31 16:34:17

dynamicC# 4.0

2009-09-01 15:25:04

C# default關(guān)

2024-12-26 00:28:59

C#base?關(guān)鍵字

2024-06-04 17:02:38

newC#編程語言

2025-01-22 08:06:38

C#yield數(shù)據(jù)迭代

2022-01-10 18:11:42

C語言應(yīng)用技巧

2011-06-14 13:26:27

volatile

2024-07-09 08:00:48

C#var?關(guān)鍵字

2021-04-18 07:58:22

SQL Server數(shù)據(jù)庫Apply

2024-05-29 14:09:00

C#編程this

2023-11-19 22:52:42

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號