面向对象基础——索引器

摘要:
1classProgram2{3staticvoidMain(string〔〕args)4{5Testtest=newTest();test〔2〕);78test〔1〕=“蔡胜彦”;test〔1}={1}”,test〔2);2021publicstringthis〔intindex〕22{23get24{25if(index==1)26{27returnname1;

  C#中的string是可以通过索引器来访问对象中的字符,但却不能修改字符的值。

  我们来看string中关于索引器的定义,如下图。

面向对象基础——索引器第1张

  上图中索引器如同属性一样,具有get方法,却没有set方法,所以这就是为什么C#中的string类型的变量都是只读的。

  

  现在让我们来编写属于自己的索引器:

 1     class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             Test test = new Test();
 6             Console.WriteLine("test[1] = {0},test[2] = {1}",test[1],test[2]);
 7 
 8             test[1] = "蔡升晏";
 9             test[2] = "石锦航";
10             Console.WriteLine("test[1] = {0},test[2] = {1}", test[1], test[2]);
11 
12             Console.WriteLine("索引器的重载:" + test["温尚翊",5]);
13         }
14     }
15 
16     public class Test 
17     {
18         private string name1 = "陈信宏";
19         private string name2 = "刘冠佑";
20 
21         public string this[int index] 
22         {
23             get
24             {
25                 if (index == 1)
26                 {
27                     return name1;
28                 }
29                 else if(index == 2)
30                 {
31                     return name2;
32                 }
33                 else
34                 {
35                     throw new Exception("索引的值超出了范围");
36                 }
37             }
38             set 
39             {
40                 if (index == 1)
41                 {
42                     name1 = value;
43                 }
44                 else if (index == 2)
45                 {
46                     name2 = value;
47                 }
48                 else
49                 {
50                     throw new Exception("索引的值超出了范围");
51                 }
52             }
53         }
54         //索引器也是可以重载的
55         public string this[string str, int x] 
56         {
57             get 
58             {
59                 return str + x;
60             }
61         }
62     }

  输出结果:

面向对象基础——索引器第2张

  我们看到,索引器像属性一样,具有get和set方法;又可以像类中的函数一样,可以被重载。

  在ADO.NET技术中,索引器经常被频繁地使用。

免责声明:文章转载自《面向对象基础——索引器》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Windows server 2008 R2 安装指引Android开发环境搭建详细图解下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

基于SpringBoot 、AOP与自定义注解转义字典值

  一直以来,前端展示字典一般以中文展示为主,若在表中存字典值中文,当字典表更改字典值对应的中文,会造成数据不一致,为此设置冗余字段并非最优方案,若由前端自己写死转义,不够灵活,若在业务代码转义,臃肿也不够通用,从网络上了解到注解、AOP是一种不错的解决方案,主要有两种方式:   1、通过注解获取结果集转为JSON字符串,通过正则查找附加字段;   2、通...

Exceptionless(二)

Exceptionless(二) - 本地部署使用进阶 作者:markjiang7m2 原文地址:https://www.cnblogs.com/markjiang7m2/p/11100563.html 官网地址:http://letyouknow.net 在上一篇文章Exceptionless - .Net Core开源日志框架中就说到如何对Except...

EXCEL导出,列表宽度大于内容列

//TIPS:列宽设置在最后 public static string Export<T>(string rootPath, List<T> list, string fileName, IDictionary<string, string> header, Action<int, string, string,...

docker 自动更新镜像和容器

原文: https://www.cnblogs.com/wangjq19920210/p/12692068.html快速开始Watch­tower 本身被打包为 Docker 镜像,因此可以像运行任何其他容器一样运行它:(然后所有容器都会自动更新,也包括 Watch­tower 本身) docker run -d --name watchtowe...

springboot整合amazonS3,封装上传文件接口

1.在pom.xml中引入amazonS3的依赖。 <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-s3</artifactId>...

利用Microsoft.Practices.Unity的拦截技术,实现.NET中的AOP

1、记住这个单词的意思:Interception(拦截) 2、首先说一下原理和背景   原理:所谓的AOP就是面向切面编程,这里不多说,百度搜索。   目的:个人认为是为了解耦,部分代码跟业务代码分离,业务代码里面不掺杂其它功能,比如:记录异常、记录操作日志。   背景:项目基本功能已完成,产品要求记录用户的操作日志,新增的时候记录某人在某时做了某事(包括...