ImageVerifierCode 换一换
格式:DOCX , 页数:16 ,大小:18.94KB ,
资源ID:10664170      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/10664170.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C#中的索引器.docx)为本站会员(b****8)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

C#中的索引器.docx

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