C# 索引器的实现过程
来源:互联网 发布:上海网络香烟销售 编辑:程序博客网 时间:2024/06/10 09:12
类的每一个属性都必须拥有一个唯一的名称,而类里定义的每一个索引器都必须拥有唯一的签名
或者参数列表,这样就可以实现索引器重载。属性可以是static,而索引器必须是实例成员。为索
引器定义的访问函数可以访问传递给索引器的参数,而属性访问函数则没有参数我们也可以为接
口定义索引器,在为接口声明索引器的时候,记住声明只是表示索引器的存在,你只需要提供恰
当的访问函数即可不必包括修饰符。
看下面案例:
ITestIndex.cs文件
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace AlgorithmDemo{ interface ITestIndex { string this[int index] { get; set; } }}TestIndex.cs文件
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace AlgorithmDemo{ class TestIndex<T> { private T[] arr = new T[100]; public T this[int i] { get { return arr[i]; } set { arr[i] = value;} } }}Program.cs
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace AlgorithmDemo{ class Program { static void Main(string[] args) { TestIndex<string> test = new TestIndex<string>(); string data = "hello,world!"; for (int i = 0; i < data.Length; i++) { test[i] = data[i].ToString(); } for (int i = 0; i < data.Length; i++) { Console.Write(test[i]); } while (true) ; } } }效果图:
0 0
- C#中索引器的实现过程
- C#中索引器的实现过程
- C#中索引器的实现过程
- C#中索引器的实现过程
- C#中索引器的实现过程
- C#中索引器的实现过程
- C#中索引器的实现过程
- C# 索引器的实现过程
- C#中索引器的实现过程 收藏
- C#中索引器的实现
- C#中简单索引器的实现
- C#的索引器
- C#的索引器
- C#索引器-索引器的定义
- C#索引器-索引器的定义
- C#索引器-索引器的定义
- C#索引器的作用
- C#索引器的使用
- Retrofit 2.0 + OkHttp 3.0
- 九、内存分配规则
- 缓冲区溢出——初学者必须注意的问题
- zynq FreeRTOS
- Android中常用的距离单位
- C# 索引器的实现过程
- 网络之HTTP协议
- RabbitMQ学习之spring配置文件rabbit标签的使用
- spark linux环境搭建
- C#播放声音【六种方法】
- QTextEdit搜索匹配文本进行高亮
- 快速排序算法——以数组第一个元素为主元
- jmeter环境搭建
- 解析百度蜘蛛如何抓取网站和提高抓取频率