1、C#中的索引器索引器是 C# 的简单组件;为数组创建索引器后,可以通过从类对象指定索引来直接访问数组元素;可以用索引数组的方式索引对象;可以像访问数组一样访问类的成员定义索引器的规则 必须指定至少一个索引器参数 应当为索引器参数赋值【索引器】索引器允许类或结构的实例就像数组一样进行索引。索引器类似于属性,不同之处在于它们的访问器采用参数。索引器在语法上方便您创建客户端应用程序可将其作为数组访问的类、结构或接口。索引器经常是在主要用于封装内部集合或数组的类型中实现的。使用索引器可以用类似于数组的方式为对象建立索引。 get 访问器返回值。set 访问器分配值。 this 关键字用于定义索引器。
2、value 关键字用于定义由 set 索引器分配的值。 索引器不必根据整数值进行索引,由您决定如何定义特定的查找机制。 索引器可被重载。 索引器可以有多个形参,例如当访问二维数组时。 view plaincopy to clipboardprint?public int thisint index / 索引声明 / get 和 set访问器 public int thisint index / 索引声明 / get 和 set访问器 注意:索引器类型及其参数类型必须至少如同索引器本身一样是可访问的。 索引器的签名由其形参的数量和类型组成。它不包括索引器类型或形参名。如果在同一类中声明一个以上的
3、索引器,则它们必须具有不同的签名。 索引器值不属于变量;因此,不能将索引器值作为 ref 或 out 参数进行传递。 view plaincopy to clipboardprint?class TempRecord / Array of temperature values private float temps = new float10 56.2F, 56.7F, 56.5F, 56.9F, 58.8F, 61.3F, 65.9F, 62.1F, 59.2F, 57.5F ; / Auto-Implemented Property System.DateTime date get; se
4、t; / To enable client code to validate input / when accessing your indexer. public int Length get return temps.Length; / Indexer declaration. / Input parameter is validated by client / code before being passed to the indexer. public float thisint index get return tempsindex; set tempsindex = value;
5、class MainClass static void Main() TempRecord tempRecord = new TempRecord(); / Use the indexers set accessor tempRecord3 = 58.3F; tempRecord5 = 60.1F; / Use the indexers get accessor for (int i = 0; i 10; i+) / This example validates the input on the client side. You may / choose to validate it in t
6、he class that implements the indexer, and throw an / exception or return an error code in the case of invalid input. if (i tempRecord.Length) System.Console.WriteLine(Element #0 = 1, i, tempRecordi); else System.Console.WriteLine(Index value of 0 is out of range, i); /Uncomment this code to see how
7、the .NET Framework handles indexer exceptions /try / / System.Console.WriteLine(Element #0 = 1, tempRecordtempRecord.Length); / /catch (System.ArgumentOutOfRangeException e) / / System.Console.WriteLine(e); / class TempRecord / Array of temperature values private float temps = new float10 56.2F, 56.
8、7F, 56.5F, 56.9F, 58.8F, 61.3F, 65.9F, 62.1F, 59.2F, 57.5F ; / Auto-Implemented Property System.DateTime date get; set; / To enable client code to validate input / when accessing your indexer. public int Length get return temps.Length; / Indexer declaration. / Input parameter is validated by client
9、/ code before being passed to the indexer. public float thisint index get return tempsindex; set tempsindex = value; class MainClass static void Main() TempRecord tempRecord = new TempRecord(); / Use the indexers set accessor tempRecord3 = 58.3F; tempRecord5 = 60.1F; / Use the indexers get accessor
10、for (int i = 0; i 10; i+) / This example validates the input on the client side. You may / choose to validate it in the class that implements the indexer, and throw an / exception or return an error code in the case of invalid input. if (i Student StudentsStudent类中,包含学生基本信息(属性)和一个方法SayHi()Students类中
11、,包含我们要创建的两种索引器,一个Student类类型的数组Code:1. / 2. /Student类具备学生基本属性一个方法SayHi() 3. / 4. publicclassStudent 5. 6. /构造函数 7. publicStudent(stringname,intage,stringhobby) 8. 9. this.Name=name; 10. this.Age=age; 11. this.Hobby=hobby; 12. 13. 14. privatestringname; 15. 16. publicstringName 17. 18. getreturnname;
12、19. setname=value; 20. 21. 22. privateintage; 23. 24. publicintAge 25. 26. getreturnage; 27. setage=value; 28. 29. 30. privatestringhobby; 31. 32. publicstringHobby 33. 34. getreturnhobby; 35. sethobby=value; 36. 37. 38. 39. /方法 40. publicvoidSayHi() 41. 42. Console.WriteLine(大家好,我是0,今年1,我喜欢2,Name,A
13、ge.ToString(),Hobby); 43. 44. Code:1. / 2. /Students类,含有两种简答索引器 3. / 4. publicclassStudents 5. 6. 7. /构造函数 8. publicStudents() 9. 10. stu0=newStudent(rose,20,dancing); 11. stu1=newStudent(jack,21,football); 12. stu2=newStudent(mimi,20,reading); 13. 14. publicStudentstu=newStudent3;/Student类型的数组 15.
14、16. 17. /简单整数索引器 18. publicStudentthisintindex 19. 20. get 21. 22. returnstuindex; 23. 24. 25. 26. /简单字符串索引器 27. publicStudentthisstringname 28. 29. get 30. 31. inti; 32. boolfound=false; 33. 34. for(i=0;istu.Length;i+) 35. 36. if(stui.Name=name) 37. 38. found=true; 39. break; 40. 41. 42. if(found) 43. 44. returnstui; 45. 46. else 47. 48. returnnull; 49. 50. 51. 52. Code:1. classProgram 2. 3. staticvoidMain(stri
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1