C#索引屬性用法實例分析
更新時間:2015年06月28日 10:05:18 作者:pythoner
這篇文章主要介紹了C#索引屬性用法,實例分析了C#聲明索引屬性的相關技巧,需要的朋友可以參考下
本文實例講述了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”中搜索 // 字數(shù)“wordCount”。如果字數(shù)小于 wordCount, // 則返回 false。將“start”和 // “l(fā)ength”設置為單詞在文本中的位置和長度: 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(); } } // 獲取包含文檔中字的計數(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; } // 獲取和設置包含文檔中的字符的索引器: public char this[int index] { get { return document.TextArray[index]; } set { document.TextArray[index] = value; } } // 獲取包含文檔中字符的計數(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#程序設計有所幫助。
相關文章
C# Socket編程實現(xiàn)簡單的局域網聊天器的示例代碼
這篇文章主要介紹了C# Socket編程實現(xiàn)簡單的局域網聊天器,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03