C#索引屬性用法實例剖析。本站提示廣大學習愛好者:(C#索引屬性用法實例剖析)文章只能為提供參考,不一定能成為您想要的結果。以下是C#索引屬性用法實例剖析正文
本文實例講述了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#法式設計有所贊助。