• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

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

OC/C/C++ 水墨上仙 2187次浏览

本示例演示 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#索引属性使用范例代码
喜欢 (0)
加载中……