C# 索引器 学习
转载原地址: http://www.cnblogs.com/lxblog/p/3940261.html
1、索引器(Indexer):
索引器允许类或者结构的实例按照与数组相同的方式进行索引。索引器类似于属性,不同之处在于他们的访问采用参数。
最简单的索引器的使用
/// <summary> /// 最简单的索引器 /// </summary> public class IDXer { private string[] name=new string[10]; //索引器必须以this关键字定义,其实这个this就是类实例化之后的对象 public string this[int index] { get { return name[index]; } set { name[index] = value; } } } public class Program { static void Main(string[] args) { //最简单索引器的使用 IDXer indexer = new IDXer(); //“=”号右边对索引器赋值,其实就是调用其set方法 indexer[0] = "张三"; indexer[1] = "李四"; //输出索引器的值,其实就是调用其get方法 Console.WriteLine(indexer[0]); Console.WriteLine(indexer[1]); Console.ReadKey(); } }
2、索引器与数组的区别:
- 索引器的索引值(Index)类型不限定为整数:
用来访问数组的索引值(Index)一定为整数,而索引器的索引值类型可以定义为其他类型。
- 索引器允许重载
一个类不限定为只能定义一个索引器,只要索引器的函数签名不同,就可以定义多个索引器,可以重载它的功能。
- 索引器不是一个变量
索引器没有直接定义数据存储的地方,而数组有。索引器具有Get和Set访问器。
3、索引器与属性的区别:
- 索引器以函数签名方式 this 来标识,而属性采用名称来标识,名称可以任意
- 索引器可以重载,而属性不能重载。
- 索引器不能用static 来进行声明,而属性可以。索引器永远属于实例成员,因此不能声明为static。
以字符串作为下标,对索引器进行存取:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | //以字符串为下标的索引器 public class IDXer2 { private Hashtable name = new Hashtable(); //以字符串为下标的索引器 public string this [ string index] { get { return name[index].ToString(); } set { name.Add(index, value); } } } public class Program { static void Main( string [] args) { //以字符串为下标的索引器 IDXer2 indexer2 = new IDXer2(); indexer2[ "A01" ] = "张三" ; indexer2[ "A02" ] = "李四" ; Console.WriteLine(indexer2[ "A01" ]); Console.WriteLine(indexer2[ "A02" ]); Console.ReadKey(); } } |
多参数索引器及索引器的重载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | /// <summary> /// 成绩类 /// </summary> public class Scores { /// <summary> /// 学生姓名 /// </summary> public string StuName { get ; set ; } /// <summary> /// 课程ID /// </summary> public int CourseId { get ; set ; } /// <summary> /// 分数 /// </summary> public int Score { get ; set ; } } /// <summary> /// 查找成绩类(索引器) /// </summary> public class FindScore { private List<Scores> listScores; public FindScore() { listScores = new List<Scores>(); } //索引器 通过名字&课程编号查找和保存成绩 public int this [ string stuName, int courseId] { get { Scores s = listScores.Find(x => x.StuName == stuName && x.CourseId == courseId); if (s != null ) { return s.Score; } else { return -1; } } set { listScores.Add( new Scores() { StuName = stuName, CourseId = courseId, Score = value }); } } //索引器重载,根据名字查找所有成绩 public List<Scores> this [ string stuName] { get { List<Scores> tempList = listScores.FindAll(x => x.StuName == stuName); return tempList; } } } static void Main( string [] args) { //多参数索引器和索引器重载 FindScore fScore = new FindScore(); fScore[ "张三" , 1] = 98; fScore[ "张三" , 2] = 100; fScore[ "张三" , 3] = 95; fScore[ "李四" , 1] = 96; //查找 张三 课程编号2 的成绩 Console.WriteLine( "李四 课程编号2 成绩为:" + fScore[ "李四" , 1]); //查找所有张三的成绩 List<Scores> listScores = fScore[ "张三" ]; if (listScores.Count > 0) { foreach (Scores s in listScores) { Console.WriteLine( string .Format( "张三 课程编号{0} 成绩为:{1}" , s.CourseId, s.Score)); } } else { Console.WriteLine( "无该学生成绩单" ); } Console.ReadKey(); } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律