首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 教程第 11 課:索引器完整指南(建立、多載、多參數、字串索引)

     編輯:關於C語言

學習目標

  1. 理解索引器概念與用途
  2. 實作單一整數索引器
  3. 多載索引器(不同參數型別)
  4. 多參數索引器

基本索引器:IntIndexer

using System;

class IntIndexer
{
    private string[] data;

    public IntIndexer(int size)
    {
        data = new string[size];
        for (int i = 0; i < size; i++) data[i] = "empty";
    }

    public string this[int pos]
    {
        get => data[pos];
        set => data[pos] = value;
    }

    static void Main()
    {
        var idx = new IntIndexer(10);
        idx[9] = "Some Value";
        idx[3] = "Another Value";
        idx[5] = "Any Value";

        Console.WriteLine("\nIndexer Output\n");
        for (int i = 0; i < 10; i++)
            Console.WriteLine($"myInd[{i}]: {idx[i]}");
    }
}

輸出

myInd[0]: empty
myInd[1]: empty
myInd[2]: empty
myInd[3]: Another Value
myInd[4]: empty
myInd[5]: Any Value
myInd[6]: empty
myInd[7]: empty
myInd[8]: empty
myInd[9]: Some Value

多載索引器:OvrIndexer

同一類別可定義多個索引器,參數型別/個數不同即可:

class OvrIndexer
{
    private string[] data;
    private int size;

    public OvrIndexer(int size)
    {
        this.size = size;
        data = new string[size];
        for (int i = 0; i < size; i++) data[i] = "empty";
    }

    // 整數索引器
    public string this[int pos]
    {
        get => data[pos];
        set => data[pos] = value;
    }

    // 字串索引器:get 回傳符合值的計數,set 將所有符合值替換
    public string this[string val]
    {
        get
        {
            int count = 0;
            for (int i = 0; i < size; i++)
                if (data[i] == val) count++;
            return count.ToString();
        }
        set
        {
            for (int i = 0; i < size; i++)
                if (data[i] == val) data[i] = value;
        }
    }

    static void Main()
    {
        var idx = new OvrIndexer(10);
        idx[9] = "Some Value";
        idx[3] = "Another Value";
        idx[5] = "Any Value";
        idx["empty"] = "no value"; // 呼叫字串索引器 set

        Console.WriteLine("\nIndexer Output\n");
        for (int i = 0; i < 10; i++)
            Console.WriteLine($"myInd[{i}]: {idx[i]}");

        Console.WriteLine($"\nNumber of \"no value\" entries: {idx["no value"]}"); // 呼叫 get
    }
}

輸出

myInd[0]: no value
myInd[1]: no value
myInd[2]: no value
myInd[3]: Another Value
myInd[4]: no value
myInd[5]: Any Value
myInd[6]: no value
myInd[7]: no value
myInd[8]: no value
myInd[9]: Some Value

Number of "no value" entries: 7

多參數索引器語法

public class Matrix
{
    private double[,] data = new double[10, 10];

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

呼叫:matrix[2, 3] = 5.5;

合法索引器參數型別

  • 整數型別 (int, long...)
  • 列舉型別
  • 字串 (string)
  • 其他實值型別/參考型別(語意上需謹慎)

小結

索引器讓類別支援陣列語法存取內部資料,支援多載與多參數,極適合集合、矩陣、字典式查詢等場景。善用索引器可大幅提升 API 易用性。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved