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;

*/

相关推荐
wxin_VXbishe9 小时前
C#(asp.net)学员竞赛信息管理系统-计算机毕业设计源码28790
java·vue.js·spring boot·spring·django·c#·php
bugcome_com21 小时前
零基础入门C#:一篇搞懂核心知识点
c#
程序员敲代码吗1 天前
如何通过命令行启动COMSOL的参数化、批处理和集群扫描
java·c#·bash
缺点内向1 天前
C#: 告别繁琐!轻松移除Word文档中的文本与图片水印
c#·自动化·word·.net
喵叔哟1 天前
06-ASPNETCore-WebAPI开发
服务器·后端·c#
2501_930707781 天前
使用 C# .NET 从 PowerPoint 演示文稿中提取背景图片
c#·powerpoint·.net
初级代码游戏1 天前
套路化编程 C# winform 自适应缩放布局
开发语言·c#·winform·自动布局·自动缩放
大空大地20261 天前
流程控制语句--switch多分支语句使用、while循环语句的使用、do...while语句、for循环
c#
kylezhao20191 天前
C#序列化与反序列化详细讲解与应用
c#
JQLvopkk1 天前
C# 实践AI :Visual Studio + VSCode 组合方案
人工智能·c#·visual studio