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ì)有所幫助。
您可能感興趣的文章:
- 輕松學(xué)習(xí)C#的屬性
- C#實(shí)現(xiàn)獲取不同對象中名稱相同屬性的方法
- C#實(shí)現(xiàn)ComboBox控件顯示出多個(gè)數(shù)據(jù)源屬性的方法
- C#中使用反射遍歷一個(gè)對象屬性及值的小技巧
- C#實(shí)現(xiàn)ProperTyGrid自定義屬性的方法
- C#屬性(Attribute)用法實(shí)例解析
- C#利用反射來判斷對象是否包含某個(gè)屬性的實(shí)現(xiàn)方法
- C#類中的屬性使用總結(jié)(詳解類的屬性)
- C#類中屬性與成員變量的使用小結(jié)
- C#中屬性和成員變量的區(qū)別說明
- 詳解C#中的屬性和屬性的使用
相關(guān)文章
C#實(shí)現(xiàn)Array添加擴(kuò)展實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)Array添加擴(kuò)展,對C#初學(xué)者有不錯的參考價(jià)值,需要的朋友可以參考下2014-08-08C#/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-03C#使用async和await實(shí)現(xiàn)異步編程
本文詳細(xì)講解了C#使用async和await實(shí)現(xiàn)異步編程的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07C# 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-03Unity使用LineRender實(shí)現(xiàn)簽名效果
這篇文章主要為大家詳細(xì)介紹了Unity使用LineRender實(shí)現(xiàn)簽名效果,制作簽名功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10