91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C#索引屬性使用范例源碼

發布時間:2020-06-30 06:59:35 來源:網絡 閱讀:412 作者:jasmine1024 欄目:編程語言

將開發過程中常用的內容記錄起來,下面的資料是關于C#索引屬性使用范例的代碼。

using System;

public class Document
{
    public class WordCollection
    {

        internal WordCollection(Document d)
        {
           document = d;
        }

        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)) 
                {
                    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
    {

        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;

    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?"
        );

        for (int i = 0; i < d.Words.Count; ++i) 
        {
            if (d.Words[i] == "peter") 
                d.Words[i] = "penelope";
        }

        for (int i = 0; i < d.Characters.Count; ++i) 
        {
            if (d.Characters[i] == 'p')
                d.Characters[i] = 'P';
        }

        Console.WriteLine(d.Text);
    }
}
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

邵阳县| 东乡| 伊通| 简阳市| 鹿泉市| 阳西县| 扶风县| 新竹县| 潍坊市| 凤城市| 正宁县| 库伦旗| 泸定县| 大石桥市| 克什克腾旗| 乌拉特前旗| 秦皇岛市| 蒙城县| 上虞市| 裕民县| 根河市| 徐州市| 和龙市| 高密市| 吉首市| 平度市| 慈溪市| 乌拉特后旗| 江永县| 安国市| 香格里拉县| 婺源县| 樟树市| 开化县| 鹤岗市| 武威市| 垦利县| 赤峰市| 铜鼓县| 楚雄市| 鹿泉市|