最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#索引屬性用法實(shí)例分析

 更新時(shí)間:2015年06月28日 10:05:18   作者:pythoner  
這篇文章主要介紹了C#索引屬性用法,實(shí)例分析了C#聲明索引屬性的相關(guān)技巧,需要的朋友可以參考下

本文實(shí)例講述了C#索引屬性的用法。分享給大家供大家參考。具體如下:

這里演示C#類如何聲明索引屬性以表示不同種類事物的類似數(shù)組的集合。

// indexedproperty.cs
using System;
public class Document
{
  // 以下類型允許文檔的查看方式與字的數(shù)組一樣:
  public class WordCollection
  {
    readonly Document document; // 包含文檔
    internal WordCollection(Document d)
    {
      document = d;
    }
    // Helper 函數(shù) -- 從字符“begin”開始在字符數(shù)組“text”中搜索
    // 字?jǐn)?shù)“wordCount”。如果字?jǐn)?shù)小于 wordCount,
    // 則返回 false。將“start”和
    // “l(fā)ength”設(shè)置為單詞在文本中的位置和長(zhǎng)度:
    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;
    }
    // 獲取和設(shè)置包含文檔中的字的索引器:
    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();
      }
    }
    // 獲取包含文檔中字的計(jì)數(shù):
    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; 
      }
    }
  }
  // 以下類型允許文檔的查看方式像字符的“數(shù)組”
  // 一樣:
  public class CharacterCollection
  {
    readonly Document document; // 包含文檔
    internal CharacterCollection(Document d)
    {
     document = d; 
    }
    // 獲取和設(shè)置包含文檔中的字符的索引器:
    public char this[int index] 
    {
      get 
      { 
        return document.TextArray[index]; 
      }
      set 
      { 
        document.TextArray[index] = value; 
      }
    }
    // 獲取包含文檔中字符的計(jì)數(shù):
    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);
  }
}

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

绥江县| 醴陵市| 湟中县| 杭州市| 西平县| 兴国县| 忻州市| 岑溪市| 永平县| 清水县| 沈阳市| 无棣县| 湛江市| 万荣县| 灯塔市| 农安县| 于都县| 鄢陵县| 嘉祥县| 米林县| 秀山| 外汇| 行唐县| 嘉荫县| 太仆寺旗| 元氏县| 灵武市| 奉节县| 鹤壁市| 祁门县| 阳曲县| 广汉市| 高雄市| 江西省| 海兴县| 崇文区| 龙泉市| 宽城| 莱西市| 福海县| 漠河县|