SoFunction
Updated on 2025-03-06

C# index attribute usage example analysis

This article describes the usage of C# index attributes. Share it for your reference. The details are as follows:

Here we demonstrate how C# class declares index properties to represent a collection of similar arrays of different kinds of things.

// 
using System;
public class Document
{
  // The following types allow the document to be viewed the same way as the word array:  public class WordCollection
  {
    readonly Document document; // Include document    internal WordCollection(Document d)
    {
      document = d;
    }
    // Helper function -- search in character array "text" starting from character "begin"    // WordCount "wordCount".  If the word count is less than wordCount,    // Returns false.  Put "start" and    // "length" is set to the position and length of the word in the text:    private bool GetWord(char[] text, int begin, int wordCount, out int start, out int length) 
    { 
      int end = ;
      int count = 0;
      int inWord = -1;
      start = length = 0; 
      for (int i = begin; i <= end; ++i) 
      {
        bool isLetter = i < end && (text[i]);
        if (inWord >= 0) 
        {
          if (!isLetter) 
          {
            if (count++ == wordCount) 
            {
              start = inWord;
              length = i - inWord;
              return true;
            }
            inWord = -1;
          }
        }
        else 
        {
          if (isLetter)
            inWord = i;
        }
      }
      return false;
    }
    // Get and set an indexer containing words in the document:    public string this[int index] 
    {
      get 
      { 
        int start, length;
        if (GetWord(, 0, index, out start, out length))
          return new string(, start, length);
        else
          throw new IndexOutOfRangeException();
      }
      set 
      {
        int start, length;
        if (GetWord(, 0, index, out start, out length)) 
        {
          // Replace the string "value" at start/length          // Character:          if (length == ) 
          {
            ((), 0, , start, length);
          }
          else 
          {
            char[] newText = 
              new char[ +  - length];
            (, 0, newText, 0, start);
            ((), 0, newText, start, );
            (, start + length, newText, start + ,  - start - length);
             = newText;
          }
        }          
        else
          throw new IndexOutOfRangeException();
      }
    }
    // Get the count of words containing the document:    public int Count 
    {
      get 
      { 
        int count = 0, start = 0, length = 0;
        while (GetWord(, start + length, 0, out start, out length))
          ++count;
        return count; 
      }
    }
  }
  // The following types allow the viewing method of documents like "array" of characters  // Same:  public class CharacterCollection
  {
    readonly Document document; // Include document    internal CharacterCollection(Document d)
    {
     document = d; 
    }
    // Get and set an indexer containing characters in the document:    public char this[int index] 
    {
      get 
      { 
        return [index]; 
      }
      set 
      { 
        [index] = value; 
      }
    }
    // Get the count containing characters in the document:    public int Count 
    {
      get 
      { 
        return ; 
      }
    }
  }
  // Since the field type has an indexer,  // So these fields are displayed as "Index Attributes":  public WordCollection Words;
  public CharacterCollection Characters;
  private char[] TextArray; // Document text.  public Document(string initialText)
  {
    TextArray = ();
    Words = new WordCollection(this);
    Characters = new CharacterCollection(this);
  }
  public string Text 
  {
    get 
    { 
      return new string(TextArray); 
    }
  }
}
class Test
{
  static void Main()
  {
    Document d = new Document(
      "peter piper picked a peck of pickled peppers. How many pickled peppers did peter piper pick?"
    );
    // Change the word "peter" to "penelope":    for (int i = 0; i < ; ++i) 
    {
      if ([i] == "peter") 
        [i] = "penelope";
    }
    // Change the character "p" to "P"    for (int i = 0; i < ; ++i) 
    {
      if ([i] == 'p')
        [i] = 'P';
    }
    ();
  }
}

I hope this article will be helpful to everyone's C# programming.