当前位置: 移动技术网 > IT编程>开发语言>c# > C#索引属性用法实例分析

C#索引属性用法实例分析

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例讲述了c#索引属性的用法。分享给大家供大家参考。具体如下:

这里演示c#类如何声明索引属性以表示不同种类事物的类似数组的集合。

// indexedproperty.cs
using system;
public class document
{
  // 以下类型允许文档的查看方式与字的数组一样:
  public class wordcollection
  {
    readonly document document; // 包含文档
    internal wordcollection(document d)
    {
      document = d;
    }
    // helper 函数 -- 从字符“begin”开始在字符数组“text”中搜索
    // 字数“wordcount”。如果字数小于 wordcount,
    // 则返回 false。将“start”和
    // “length”设置为单词在文本中的位置和长度:
    private bool getword(char[] text, int begin, int wordcount, out int start, out int length) 
    { 
      int end = text.length;
      int count = 0;
      int inword = -1;
      start = length = 0; 
      for (int i = begin; i <= end; ++i) 
      {
        bool isletter = i < end && char.isletterordigit(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;
    }
    // 获取和设置包含文档中的字的索引器:
    public string this[int index] 
    {
      get 
      { 
        int start, length;
        if (getword(document.textarray, 0, index, out start, out length))
          return new string(document.textarray, start, length);
        else
          throw new indexoutofrangeexception();
      }
      set 
      {
        int start, length;
        if (getword(document.textarray, 0, index, out start, out length)) 
        {
          // 用字符串“value”替换位于 start/length 处的
          // 字:
          if (length == value.length) 
          {
            array.copy(value.tochararray(), 0, document.textarray, start, length);
          }
          else 
          {
            char[] newtext = 
              new char[document.textarray.length + value.length - length];
            array.copy(document.textarray, 0, newtext, 0, start);
            array.copy(value.tochararray(), 0, newtext, start, value.length);
            array.copy(document.textarray, start + length, newtext, start + value.length, document.textarray.length - start - length);
            document.textarray = newtext;
          }
        }          
        else
          throw new indexoutofrangeexception();
      }
    }
    // 获取包含文档中字的计数:
    public int count 
    {
      get 
      { 
        int count = 0, start = 0, length = 0;
        while (getword(document.textarray, start + length, 0, out start, out length))
          ++count;
        return count; 
      }
    }
  }
  // 以下类型允许文档的查看方式像字符的“数组”
  // 一样:
  public class charactercollection
  {
    readonly document document; // 包含文档
    internal charactercollection(document d)
    {
     document = d; 
    }
    // 获取和设置包含文档中的字符的索引器:
    public char this[int index] 
    {
      get 
      { 
        return document.textarray[index]; 
      }
      set 
      { 
        document.textarray[index] = value; 
      }
    }
    // 获取包含文档中字符的计数:
    public int count 
    {
      get 
      { 
        return document.textarray.length; 
      }
    }
  }
  // 由于字段的类型具有索引器,
  // 因此这些字段显示为“索引属性”:
  public wordcollection words;
  public charactercollection characters;
  private char[] textarray; // 文档的文本。
  public document(string initialtext)
  {
    textarray = initialtext.tochararray();
    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?"
    );
    // 将字“peter”更改为“penelope”:
    for (int i = 0; i < d.words.count; ++i) 
    {
      if (d.words[i] == "peter") 
        d.words[i] = "penelope";
    }
    // 将字符“p”更改为“p”
    for (int i = 0; i < d.characters.count; ++i) 
    {
      if (d.characters[i] == 'p')
        d.characters[i] = 'p';
    }
    console.writeline(d.text);
  }
}

希望本文所述对大家的c#程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网