欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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è)置為單詞在文本中的位置和長度:
    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);
  }
}

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

相關(guān)文章

  • C#實(shí)現(xiàn)Array添加擴(kuò)展實(shí)例

    C#實(shí)現(xiàn)Array添加擴(kuò)展實(shí)例

    這篇文章主要介紹了C#實(shí)現(xiàn)Array添加擴(kuò)展,對C#初學(xué)者有不錯的參考價(jià)值,需要的朋友可以參考下
    2014-08-08
  • C# Console類的具體用法

    C# Console類的具體用法

    這篇文章主要介紹C# Console類的具體用法,需要的朋友可以參考下
    2013-03-03
  • C#/VB.NET實(shí)現(xiàn)將XML轉(zhuǎn)為PDF

    C#/VB.NET實(shí)現(xiàn)將XML轉(zhuǎn)為PDF

    可擴(kuò)展標(biāo)記語言(XML)文件是一種標(biāo)準(zhǔn)的文本文件,它使用特定的標(biāo)記來描述文檔的結(jié)構(gòu)以及其他特性。本文將利用C#實(shí)現(xiàn)XML文件轉(zhuǎn)PDF?,需要的可以參考一下
    2022-03-03
  • C#使用async和await實(shí)現(xiàn)異步編程

    C#使用async和await實(shí)現(xiàn)異步編程

    本文詳細(xì)講解了C#使用async和await實(shí)現(xiàn)異步編程的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • C# Socket編程實(shí)現(xiàn)簡單的局域網(wǎng)聊天器的示例代碼

    C# Socket編程實(shí)現(xiàn)簡單的局域網(wǎng)聊天器的示例代碼

    這篇文章主要介紹了C# Socket編程實(shí)現(xiàn)簡單的局域網(wǎng)聊天器,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • C#簡單輸出日歷的方法

    C#簡單輸出日歷的方法

    這篇文章主要介紹了C#簡單輸出日歷的方法,涉及C#針對日期與時(shí)間的簡單操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • C# WPF ListView控件的實(shí)例詳解

    C# WPF ListView控件的實(shí)例詳解

    這篇文章主要介紹了C# WPF ListView控件的實(shí)例詳解的相關(guān)資料,希望通過本能幫助到大家,讓大家掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-10-10
  • C#中XML基礎(chǔ)用法

    C#中XML基礎(chǔ)用法

    可擴(kuò)展標(biāo)記語言,標(biāo)準(zhǔn)通用標(biāo)記語言的子集,簡稱XML。是一種用于標(biāo)記電子文件使其具有結(jié)構(gòu)性的標(biāo)記語言。這篇文章介紹了C#中XML基礎(chǔ)的用法,下面的實(shí)例代碼,大家可以看看
    2021-12-12
  • C#往線程里傳遞參數(shù)的方法小結(jié)

    C#往線程里傳遞參數(shù)的方法小結(jié)

    這篇文章主要介紹了C#往線程里傳參數(shù)的方法小結(jié)的相關(guān)資料,還給大家簡單說明下傳參的兩種方式,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-07-07
  • Unity使用LineRender實(shí)現(xiàn)簽名效果

    Unity使用LineRender實(shí)現(xiàn)簽名效果

    這篇文章主要為大家詳細(xì)介紹了Unity使用LineRender實(shí)現(xiàn)簽名效果,制作簽名功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10

最新評論