欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

C#索引指示器怎么使用

這篇文章主要講解了“C#索引指示器怎么使用”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“C#索引指示器怎么使用”吧!

成都創(chuàng)新互聯(lián)憑借專業(yè)的設(shè)計(jì)團(tuán)隊(duì)扎實(shí)的技術(shù)支持、優(yōu)質(zhì)高效的服務(wù)意識(shí)和豐厚的資源優(yōu)勢(shì),提供專業(yè)的網(wǎng)站策劃、網(wǎng)站建設(shè)、成都網(wǎng)站制作、網(wǎng)站優(yōu)化、軟件開發(fā)、網(wǎng)站改版等服務(wù),在成都10年的網(wǎng)站建設(shè)設(shè)計(jì)經(jīng)驗(yàn),為成都上千余家中小型企業(yè)策劃設(shè)計(jì)了網(wǎng)站。

C#索引指示器并不難使用。它們的用法跟數(shù)組相同。在一個(gè)類內(nèi)部,你可以按照你的意愿來管理一組數(shù)據(jù)的集合。這些對(duì)象可以是類成員的有限集合,也可以是另外一個(gè)數(shù)組,或者是一些復(fù)雜的數(shù)據(jù)結(jié)構(gòu)。不考慮類的內(nèi)部實(shí)現(xiàn),其數(shù)據(jù)可以通過使用C#索引指示器來獲得。

實(shí)現(xiàn)C#索引指示器(indexer)的類可以象數(shù)組那樣使用其實(shí)例后的對(duì)象,但與數(shù)組不同的是C#索引指示器的參數(shù)類型不僅限于int。簡單來說,其本質(zhì)就是一個(gè)含參數(shù)屬性:

  1. using System;  

  2. using System.Collections.Generic;  

  3. using System.Text;  

  4.    

  5. namespace Example08  

  6. {  

  7. public class Point  

  8. {  

  9. private double x, y;  

  10. public Point(double X, double Y)  

  11. {  

  12. x = X;  

  13. y = Y;  

  14. }  

  15. //重寫ToString方法方便輸出  

  16. public override string ToString()  

  17. {  

  18. return String.Format("X: {0} , Y: {1}", x, y);  

  19. }  

  20. }  

  21. public class Points  

  22. {  

  23. Point[] points;  

  24. public Points(Point[] Points)  

  25. {  

  26. points = Points;  

  27. }  

  28. public int PointNumber  

  29. {  

  30. get   

  31. {   

  32. return points.Length;   

  33. }  

  34. }  

  35. //實(shí)現(xiàn)索引訪問器  

  36. public Point this[int Index]  

  37. {  

  38. get  

  39. {  

  40. return points[Index];  

  41. }  

  42. }  

  43. }  

  44.    

  45. //感謝watson hua(http://huazhihao.cnblogs.com/)的指點(diǎn)  

  46. //索引指示器的實(shí)質(zhì)是含參屬性,參數(shù)并不只限于int  

  47. class WeatherOfWeek  

  48. {  

  49. public string this[int Index]  

  50. {  

  51. get  

  52. {  

  53. //注意case段使用return直接返回所以不需要break  

  54. switch (Index)  

  55. {  

  56. case 0:  

  57. {  

  58. return "Today is cloudy!";  

  59. }  

  60. case 5:  

  61. {  

  62. return "Today is thundershower!";  

  63. }  

  64. default:  

  65. {  

  66. return "Today is fine!";  

  67. }  

  68. }  

  69. }  

  70. }  

  71. public string this[string Day]  

  72. {  

  73. get  

  74. {  

  75. string TodayWeather = null;  

  76. //switch的標(biāo)準(zhǔn)寫法  

  77. switch (Day)  

  78. {  

  79. case "Sunday":  

  80. {  

  81. TodayWeather = "Today is cloudy!";  

  82. break;  

  83. }  

  84. case "Friday":  

  85. {  

  86. TodayWeather = "Today is thundershower!";  

  87. break;  

  88. }  

  89. default:  

  90. {  

  91. TodayWeather = "Today is fine!";  

  92. break;  

  93. }  

  94. }  

  95. return TodayWeather;  

  96. }  

  97. }  

  98. }  

  99. class Program  

  100. {  

  101. static void Main(string[] args)  

  102. {  

  103. Point[] tmpPoints = new Point[10];  

  104. for (int i = 0; i < tmpPoints.Length; i++)  

  105. {  

  106. tmpPoints[i] = new Point(i, Math.Sin(i));  

  107. }  

  108. Points tmpObj = new Points(tmpPoints);  

  109. for (int i = 0; i < tmpObj.PointNumber; i++)  

  110. {  

  111. Console.WriteLine(tmpObj[i]);  

  112. }  

  113. string[] Week = new string[] 
    { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Staurday"};  

  114. WeatherOfWeek tmpWeatherOfWeek = new WeatherOfWeek();  

  115. for (int i = 0; i < 6; i++)  

  116. {  

  117. Console.WriteLine(tmpWeatherOfWeek[i]);  

  118. }  

  119. foreach (string tmpDay in Week)  

  120. {  

  121. Console.WriteLine(tmpWeatherOfWeek[tmpDay]);  

  122. }  

  123. Console.ReadLine();  

  124. }  

  125. }  

感謝各位的閱讀,以上就是“C#索引指示器怎么使用”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)C#索引指示器怎么使用這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

分享題目:C#索引指示器怎么使用
當(dāng)前鏈接:http://www.chinadenli.net/article22/iiehjc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)靜態(tài)網(wǎng)站微信小程序外貿(mào)網(wǎng)站建設(shè)App開發(fā)網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)