C#索引器 接口

索引器总结

一、索引器基础概念

索引器 是C#中的一种特殊成员,它允许对象像数组一样使用 [] 运算符来访问元素。

基本语法

csharp

复制代码
public 返回类型 this[参数类型 参数名]
{
    get { return 数据; }    // 获取数据时执行
    set { 数据 = value; }   // 设置数据时执行,value是关键字
}

二、索引器的基本用法

1. 单参数索引器(最常用)

csharp

复制代码
public class MyCollection
{
    private List<string> items = new List<string>();
    
    // 索引器:通过int索引访问
    public string this[int index]
    {
        get => items[index];
        set => items[index] = value;
    }
}

// 使用
var collection = new MyCollection();
collection[0] = "第一项";  // 设置
string item = collection[0]; // 获取

2. 多参数索引器

csharp

复制代码
public class Matrix
{
    private int[,] data = new int[10, 10];
    
    public int this[int row, int col]
    {
        get => data[row, col];
        set => data[row, col] = value;
    }
}

// 使用
var matrix = new Matrix();
matrix[2, 3] = 100;
int val = matrix[2, 3];

3. 字符串参数索引器

csharp

复制代码
public class StudentCollection
{
    private List<Student> students = new List<Student>();
    
    // 通过名字查找学生
    public Student this[string name]
    {
        get => students.Find(s => s.Name == name);
        set 
        {
            int index = students.FindIndex(s => s.Name == name);
            if (index >= 0)
                students[index] = value;
        }
    }
}

三、索引器重载

同一个类中可以定义多个索引器,只要参数类型或数量不同:

csharp

复制代码
public class SmartCollection
{
    private List<Student> students = new List<Student>();
    
    // 重载1:通过索引访问
    public Student this[int index]
    {
        get => students[index];
        set => students[index] = value;
    }
    
    // 重载2:通过名字访问
    public Student this[string name]
    {
        get => students.Find(s => s.Name == name);
        set 
        {
            int index = students.FindIndex(s => s.Name == name);
            if (index >= 0) students[index] = value;
        }
    }
    
    // 重载3:通过ID访问
    public Student this[int id, string type]  // type参数用于区分
    {
        get => students.Find(s => s.ID == id);
    }
}

四、索引器与属性的区别

特性 索引器 属性
语法 this[参数] 自定义名称
参数 必须有(1个或多个) 无参数
调用方式 obj[索引] obj.PropertyName
重载 可以(基于参数) 不可以
用途 访问集合元素 访问对象状态

五、总结

索引器的核心价值:

  1. 提供直观的访问语法:让自定义类型拥有类似数组的访问方式

  2. 增强代码可读性 :config["Key"] 比 config.GetValue("Key") 更直观

  3. 支持多维度访问:可以定义多维或多参数的索引器

  4. 实现接口契约:实现集合接口时必需

使用原则:

  1. 索引器适合"元素访问"语义

  2. 保持索引器逻辑简单

  3. 重要操作使用命名方法更清晰

  4. 添加适当的边界检查和异常处理

索引器是C#中非常有用的特性,特别是在创建集合类、配置管理、数据访问层等场景中,能显著提升代码的易用性和表达力。

接口和抽象类的区别

/* 相同的:都不能实例化,

* 都可以让派生类的对象赋值给抽象类或者接口类型变量 A1 a1 = new A1(); IPeople i1 = new IPeople();

* 都得必须在子类实现

* 不同点:

1:抽象类可以包含普通方法和成员 也可以抽象成员和方法, 接口只能有未实行的成员和方法

2: 接口可以多继承 抽象类还是和普通类的一样的继承

3:抽象类在子类通过override进行实现 接口直接使用即可

4: 抽象类修饰符可以添加修饰符 ,接口里面不能添加修饰符

5: 抽象类可以定义字段 接口里面不能定义字段 例如字段:int age;

*/

相关推荐
暴走约伯5 小时前
【VFX工具链源码拆解 09】终极工业化闭环:基于 ID 规则驱动的全自动场景建档、骨骼挂载与男转女场景迁移
unity·c#·游戏引擎·材质
用户788477316341 天前
WinForms 迁移 WPF:Upgrade Assistant 覆盖 30%,剩下 70% 我手写了六周
c#
寂寞的船1 天前
【Unity知识速递】第13期 Unity 内存泄漏排查实战:从 Memory Profiler 到 Lua 引用泄漏的生产级全链路诊断
unity·junit·c#·游戏引擎·lua
用户3721574261352 天前
C# 提取 PDF 附件:包括文档附件和页面注释附件
c#
寂寞的船2 天前
【Unity Bug挑战】第12期 Awaitable 二次 await 死锁之谜 —— 为什么同一个异步操作等两次就卡死?
unity·c#·游戏引擎·bug
huaqianzkh3 天前
WinForm C#:循环修改控件UI闪烁卡顿?SuspendLayout / ResumeLayout 与 BeginUpdate 优化
开发语言·ui·c#
用户3721574261354 天前
C# 实现 Excel 单元格保护:锁定公式并允许编辑指定区域
c#·ai编程·编程语言
公子小六4 天前
基于.NET的Windows窗体编程之WinForms数据持久化(1)
windows·microsoft·c#·.net·winforms
三雷科技4 天前
第6章第4节:自定义分词器
开发语言·c#
zhanghaha13144 天前
C# 系列教程_12 字符串 string 保姆级系统教程(面向新手,大量示例 + 原理讲解)
开发语言·数据库·c#