亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> C语言

C# 索引器完全指南:让对象像数组一样访问、重载与多参数索引器

索引器允许对象像数组一样被索引。为类定义索引器后,该类表现如虚拟数组。可使用数组访问操作符 [ ] 访问类实例。

语法

一维索引器语法:


element-type this[int index] {
    get { /* 返回 index 指定的值 */ }
    set { /* 设置 index 指定的值 */ }
}

索引器用法

声明类似属性,使用 get/set 访问器。但属性返回/设置特定数据成员,索引器返回/设置对象实例的特定值——将实例数据分割成更小部分并索引。

属性有名称,索引器无名称,用 this 关键字指代对象实例。

基本示例


using System;
namespace IndexerApplication {
    class IndexedNames {
        private string[] namelist = new string[size];
        static public int size = 10;
        
        public IndexedNames() {
            for (int i = 0; i < size; i++) namelist[i] = "N. A.";
        }
        
        public string this[int index] {
            get {
                string tmp;
                if (index >= 0 && index <= size - 1) tmp = namelist[index];
                else tmp = "";
                return tmp;
            }
            set {
                if (index >= 0 && index <= size - 1) namelist[index] = value;
            }
        }
        
        static void Main(string[] args) {
            IndexedNames names = new IndexedNames();
            names[0] = "Zara";
            names[1] = "Riz";
            names[2] = "Nuha";
            names[3] = "Asif";
            names[4] = "Davinder";
            names[5] = "Sunil";
            names[6] = "Rubic";
            
            for (int i = 0; i < IndexedNames.size; i++) Console.WriteLine(names[i]);
            Console.ReadKey();
        }
    }
}

输出:


Zara
Riz
Nuha
Asif
Davinder
Sunil
Rubic
N. A.
N. A.
N. A.

重载索引器

索引器可重载。可声明多参数,每参数类型可不同。索引不限于整数,如字符串。

多参数/字符串索引器示例


using System;
namespace IndexerApplication {
    class IndexedNames {
        private string[] namelist = new string[size];
        static public int size = 10;
        
        public IndexedNames() {
            for (int i = 0; i < size; i++) namelist[i] = "N. A.";
        }
        
        // 整数索引器
        public string this[int index] {
            get {
                if (index >= 0 && index < size) return namelist[index];
                return "";
            }
            set {
                if (index >= 0 && index < size) namelist[index] = value;
            }
        }
        
        // 字符串索引器:按名字查找索引
        public int this[string name] {
            get {
                for (int i = 0; i < size; i++)
                    if (namelist[i] == name) return i;
                return -1;  // 未找到
            }
        }
        
        static void Main(string[] args) {
            IndexedNames names = new IndexedNames();
            names[0] = "Zara"; names[1] = "Riz"; names[2] = "Nuha";
            names[3] = "Asif"; names[4] = "Davinder"; names[5] = "Sunil"; names[6] = "Rubic";
            
            // 使用整数索引器
            for (int i = 0; i < IndexedNames.size; i++) Console.WriteLine(names[i]);
            
            // 使用字符串索引器
            Console.WriteLine(names["Nuha"]);  // 2
            Console.ReadKey();
        }
    }
}

输出:


Zara
Riz
Nuha
Asif
Davinder
Sunil
Rubic
N. A.
N. A.
N. A.
2

多维索引器

支持多个参数,模拟多维数组:


public double this[int row, int col] {
    get { return matrix[row, col]; }
    set { matrix[row, col] = value; }
}

// 使用
Matrix m = new Matrix(3, 3);
m[1, 2] = 3.14;
double val = m[1, 2];

索引器 vs 属性

特性属性索引器
标识名称this 关键字
参数无至少一个(索引)
访问方式obj.Propobj[index]
重载不可重载可重载(不同参数列表)
静态可为 static不能为 static
用途单值特征集合/容器元素访问

接口中声明索引器


public interface IStringList {
    string this[int index] { get; set; }
    int Count { get; }
}

public class StringList : IStringList {
    private List list = new List();
    public string this[int index] {
        get => list[index];
        set => list[index] = value;
    }
    public int Count => list.Count;
}

最佳实践

C语言

  1. C# Hello World — 构建你的第一个C#应用程序
  2. C#关键字与标识符:规则、完整列表与最佳实践
  3. 精通C#变量与基础数据类型:完整指南
  4. C#运算符——C#运算符全面指南
  5. 掌握 C# 位运算与移位运算符:全面指南
  6. C# 基础:输入输出核心技巧完全指南
  7. C# 表达式、语句与代码块:全面指南与实战示例
  8. 掌握 C# 注释:类型、最佳实践与 XML 文档注释
  9. 掌握 C# switch 语句:语法、示例与最佳实践
  10. C# 三元运算符:条件表达式简明指南
  11. C# break 语句:用法、时机与实战示例
  12. 掌握 C# continue 语句:高效跳过循环迭代的实战指南