[C#] C#索引属性使用范例代码 →→→→→进入此内容的聊天室

来自 , 2020-03-07, 写在 C#, 查看 106 次.
URL http://www.code666.cn/view/cf34645d
  1. // indexedproperty.cs
  2. using System;
  3.  
  4. public class Document
  5. {
  6.     // 以下类型允许文档的查看方式与字的数组一样:
  7.     public class WordCollection
  8.     {
  9.         readonly Document document;  // 包含文档
  10.  
  11.         internal WordCollection(Document d)
  12.         {
  13.            document = d;
  14.         }
  15.  
  16.         // Helper 函数 -- 从字符“begin”开始在字符数组“text”中搜索
  17.         // 字数“wordCount”。如果字数小于 wordCount,
  18.         // 则返回 false。将“start”和
  19.         // “length”设置为单词在文本中的位置和长度:
  20.         private bool GetWord(char[] text, int begin, int wordCount,
  21.                                        out int start, out int length)
  22.         {
  23.             int end = text.Length;
  24.             int count = 0;
  25.             int inWord = -1;
  26.             start = length = 0;
  27.  
  28.             for (int i = begin; i <= end; ++i)
  29.             {
  30.                 bool isLetter = i < end && Char.IsLetterOrDigit(text[i]);
  31.  
  32.                 if (inWord >= 0)
  33.                 {
  34.                     if (!isLetter)
  35.                     {
  36.                         if (count++ == wordCount)
  37.                         {
  38.                             start = inWord;
  39.                             length = i - inWord;
  40.                             return true;
  41.                         }
  42.                         inWord = -1;
  43.                     }
  44.                 }
  45.                 else
  46.                 {
  47.                     if (isLetter)
  48.                         inWord = i;
  49.                 }
  50.             }
  51.             return false;
  52.         }
  53.  
  54.         // 获取和设置包含文档中的字的索引器:
  55.         public string this[int index]
  56.         {
  57.             get
  58.             {
  59.                 int start, length;
  60.                 if (GetWord(document.TextArray, 0, index, out start,
  61.                                                           out length))
  62.                     return new string(document.TextArray, start, length);
  63.                 else
  64.                     throw new IndexOutOfRangeException();
  65.             }
  66.             set
  67.             {
  68.                 int start, length;
  69.                 if (GetWord(document.TextArray, 0, index, out start,
  70.                                                          out length))
  71.                 {
  72.                     // 用字符串“value”替换位于 start/length 处的
  73.                     // 字:
  74.                     if (length == value.Length)
  75.                     {
  76.                         Array.Copy(value.ToCharArray(), 0,
  77.                                  document.TextArray, start, length);
  78.                     }
  79.                     else
  80.                     {
  81.                         char[] newText =
  82.                             new char[document.TextArray.Length +
  83.                                            value.Length - length];
  84.                         Array.Copy(document.TextArray, 0, newText,
  85.                                                         0, start);
  86.                         Array.Copy(value.ToCharArray(), 0, newText,
  87.                                              start, value.Length);
  88.                         Array.Copy(document.TextArray, start + length,
  89.                                    newText, start + value.Length,
  90.                                   document.TextArray.Length - start
  91.                                                             - length);
  92.                         document.TextArray = newText;
  93.                     }
  94.                 }                    
  95.                 else
  96.                     throw new IndexOutOfRangeException();
  97.             }
  98.         }
  99.  
  100.         // 获取包含文档中字的计数:
  101.         public int Count
  102.         {
  103.             get
  104.             {
  105.                 int count = 0, start = 0, length = 0;
  106.                 while (GetWord(document.TextArray, start + length, 0,
  107.                                               out start, out length))
  108.                     ++count;
  109.                 return count;
  110.             }
  111.         }
  112.     }
  113.  
  114.     // 以下类型允许文档的查看方式像字符的“数组”
  115.     // 一样:
  116.     public class CharacterCollection
  117.     {
  118.         readonly Document document;  // 包含文档
  119.  
  120.         internal CharacterCollection(Document d)
  121.         {
  122.           document = d;
  123.         }
  124.  
  125.         // 获取和设置包含文档中的字符的索引器:
  126.         public char this[int index]
  127.         {
  128.             get
  129.             {
  130.                 return document.TextArray[index];
  131.             }
  132.             set
  133.             {
  134.                 document.TextArray[index] = value;
  135.             }
  136.         }
  137.  
  138.         // 获取包含文档中字符的计数:
  139.         public int Count
  140.         {
  141.             get
  142.             {
  143.                 return document.TextArray.Length;
  144.             }
  145.         }
  146.     }
  147.  
  148.     // 由于字段的类型具有索引器,
  149.     // 因此这些字段显示为“索引属性”:
  150.     public WordCollection Words;
  151.     public CharacterCollection Characters;
  152.  
  153.     private char[] TextArray;  // 文档的文本。
  154.  
  155.     public Document(string initialText)
  156.     {
  157.         TextArray = initialText.ToCharArray();
  158.         Words = new WordCollection(this);
  159.         Characters = new CharacterCollection(this);
  160.     }
  161.  
  162.     public string Text
  163.     {
  164.         get
  165.         {
  166.            return new string(TextArray);
  167.         }
  168.     }
  169. }
  170.  
  171. class Test
  172. {
  173.     static void Main()
  174.     {
  175.         Document d = new Document(
  176.            "peter piper picked a peck of pickled peppers. How many pickled peppers did peter piper pick?"
  177.         );
  178.  
  179.         // 将字“peter”更改为“penelope”:
  180.         for (int i = 0; i < d.Words.Count; ++i)
  181.         {
  182.             if (d.Words[i] == "peter")
  183.                 d.Words[i] = "penelope";
  184.         }
  185.  
  186.         // 将字符“p”更改为“P”
  187.         for (int i = 0; i < d.Characters.Count; ++i)
  188.         {
  189.             if (d.Characters[i] == 'p')
  190.                 d.Characters[i] = 'P';
  191.         }
  192.        
  193.         Console.WriteLine(d.Text);
  194.     }
  195. }
  196.  
  197. //csharp/4915

回复 "C#索引属性使用范例代码"

这儿你可以回复上面这条便签

captcha