15索引器
2016-12-14 17:01
330 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _15索引器 { class Program { static void Main(string[] args) { //int[] numbers = { 1, 2, 3, 4, 5 }; //Console.WriteLine(numbers[2]); //打印:3 //Dictionary<string, int> dic = new Dictionary<string, int>(); //dic.Add("zhangsan",100); //dic.Add("andu", 60); //Console.WriteLine(dic["andu"]);//打印 60 Person p = new Person(); //p.Numbers = new int[5] { 3, 6, 7, 8, 9 }; //foreach (var item in p.Numbers) //{ // Console.WriteLine(item); //} //for (int i = 0; i < p.Numbers.Length; i++) //{ // Console.WriteLine(p.Numbers[i]); //} //不使用属性访问时,需要使用索引器 //索引器,让对象以索引的方式操作数组 p[0] = 1; p[1] = 2; p[2] = 3; p["zhangsan"] = "好人啊"; p["andu"] = "好人卡"; } } class Person { private int[] numbers = new int[5];//默认值都为0 public int[] Numbers { get { return numbers; } set { numbers = value; } } //索引器必须叫 this //索引器的写法和属性类似 public int this[int index] { get { return numbers[index]; } set { numbers[index] = value; } } Dictionary<string, string> dic = new Dictionary<string, string>(); public string this[string index] { get { return dic[index]; } set { dic[index] = value; } } //Dictionary<int,string> dic2 = new Dictionary<int,string>(); //public string this[int index] //类型一样 //{ //} } }
相关文章推荐
- 普通sqlite和谷歌API操作数据区别小结
- [置顶] 记录:Android api 24+的TabLayout问题处理
- 如何使用 Git 管理二进制大对象
- BitmapFactory.decodeXXX()方法小结
- IIS和Tomcat共用80端口的解决方案
- Web前端开发人员和设计师必读文章推荐【系列一】至【系列十二】
- Android 源码在线查看
- Android 镂空效果的遮罩层实现
- 类中implements Serializable有什么作用
- 内容解析者调用提供者套路
- Intellij IDEA常用配置详解
- Java 8 最佳技巧
- UML---类图和对象图
- IOS开发中,SVN如何恢复到某一个版本(以Cornerstone为例)
- Linux内核检测USB事件
- Golang中的信号处理
- 应用框架层Manager套路
- DataFrame格式化
- SSL/TLS协议运行机制
- Android Can't convert to dimension: type=0x1