Intern Day83 - C# - 索引器Indexer的使用原因+作用+使用
为什么引入索引器
一般属性只能访问单一的字段(一个属性对一个字段进行封装),如果想访问多个数据成员,就需要使用索引器。索引器是类的特殊成员,它可以根据索引在多个成员中进行选择,能够让对象以类似数组的方式来存取。而这种方式就叫 索引器
。
作用
-
索引器(Indexer) 。
-
索引器允许类中对象可以像数组一样使用下标的方式来访问数据(更方便、直观的被引用)。(可以当做虚拟数组)
-
可以使用数组访问运算符
[ ]
来访问该类的实例。
规则
-
索引器的数据类型必须是统一的
-
索引器的行为的声明在某种程度上类似于属性(property)。属性可使用 get 和 set 访问器来定义索引器。但是属性返回或设置的是一个特定的数据成员,而索引器返回或设置对象实例的一个特定值。
-
关键字:this。(索引器必须以this关键字定义,this 是类实例化之后的对象)
注意
本文只讲了一维索引器,重载索引后续碰到了再写。
基本语法
一维索引器的定义:
[修饰符] 数据类型 this[索引类型 index]
{
get // get 访问器
{
// 返回index指定的值,往往跟索引的不同返回不同的字段,用选择语句
}
set // set 访问器
{
//设置设置index指定的值(赋值语句)
}
}
具体使用例子
using System;
using System.Diagnostics;
using System.Reflection;
using System.Security.Cryptography;
namespace Practice1 // namespace 索引器
{
class Student // public class Student
{
private string _name, _gender,_tel; // 该字段格式:private readonly People _people;
//索引器语法格式:访问修饰符 数据类型 this[索引器类型 index]
public string this[int index]
{
get
{
switch (index)
{
case 1: return _name; // step10
case 2: return _gender; // step11
case 3: return _tel; // step2
default: throw new ArgumentOutOfRangeException("index is wrong!");//抛出异常
}
}
set
{
switch (index)
{
case 1: _name = value;break; // step3
case 2: _gender = value; break; // step5
case 3: _tel = value; break; // step7
// 这边的value关键字:表示从外部接收一个新的值,并将值指向该属性
default: throw new ArgumentOutOfRangeException("index is wrong!");//抛出异常
}
}
}
public void Show() // step9 进step9的时候去get找每个变量对应的值
{
Console.WriteLine("name = {0},gender = {1},tel = {2}", this[1], this[2], this[3]); // step10 11 12
//this[] this表示的是索引器,this[]表示访问对应字段。
}
}
class Program
{
static void Main(string[] args)
{
Student s = new Student(); // step1
s[1] = "Nicole"; // step2 //给索引器以数组的方式赋值
s[2] = "girl"; // step4
s[3] = "123456789"; // step6
// 这三个赋值分别走set方法
s.Show(); // step8
// 输出的时候分别走get方法
Console.ReadKey(); // step13
}
}
}
// 或者也可以单独把Program部分放在上面那个namespace下,其他的放在下面这个namespace里面,效果一样的
// namespace Practice1 // namespace 索引器
// {
// class Student // public class A
// {
// private string _name, _gender,_tel; // private readonly People _people;
//
// //索引器语法格式:访问修饰符 数据类型 this[索引器类型 index]
// public string this[int index]
// {
// get
// {
// switch (index)
// {
// case 1: return _name;
// case 2: return _gender;
// case 3: return _tel;
// default: throw new ArgumentOutOfRangeException("index");//抛出异常
// }
// }
// set
// {
// switch (index)
// {
// case 1: _name = value; break;
// case 2: _gender = value; break;
// case 3: _tel = value; break;
// default: throw new ArgumentOutOfRangeException("index");//抛出异常
// }
// }
// }
// public void Show()
// {
// Console.WriteLine("name = {0},gender = {1},tel = {2}", this[1], this[2], this[3]);
// //this[] this表示的是索引器,this[]表示访问向对应的字段。
// }
// }
// }
输出:
name = Nicole,gender = girl,tel = 123456789
分类:
.NET后端开发
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」