ylbtech-LanguageSamples-Indexers_2(索引器)

ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-Indexers_2(索引器)
1.A,示例(Sample) 返回顶部

Indexers_2 示例

本示例演示 C# 类如何声明索引器,以表示不同种类事物的类似数组的集合。 有关其他信息,请参见属性(C# 编程指南)。

安全说明

提供此代码示例是为了阐释一个概念,它并不代表最安全的编码实践,因此不应在应用程序或网站中使用此代码示例。 对于因将此代码示例用于其他用途而发生的偶然或必然损害,Microsoft 不承担任何责任。

在 Visual Studio 中生成并运行 Indexers_2 示例

  • 在“调试”菜单上,单击“开始执行(不调试)”。

从命令行生成并运行 Indexers_2 示例

  • 在命令提示符处,键入以下命令:

    csc indexers_2.cs
    indexers_2
1.B,示例代码(Sample Code)返回顶部

1.B.1, Indexers_2.cs

// 版权所有(C) Microsoft Corporation。保留所有权利。
// 此代码的发布遵从
// Microsoft 公共许可(MS-PL,http://opensource.org/licenses/ms-pl.html)的条款。
//
//版权所有(C) Microsoft Corporation。保留所有权利。

// 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);
    }
}

1.B.2,

1.C,下载地址(Free Download)返回顶部
作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
时间: 2024-08-27 09:46:22

ylbtech-LanguageSamples-Indexers_2(索引器)的相关文章

ylbtech-LanguageSamples-Indexers(索引器)

ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-Indexers(索引器) 1.A,示例(Sample) 返回顶部 “索引器”示例 本示例演示 C# 类如何声明索引器以提供对类的类似数组的访问. 安全说明 提供此代码示例是为了阐释一个概念,它并不代表最安全的编码实践,因此不应在应用程序或网站中使用此代码示例.对于因将此代码示例用于其他用途而出现的偶然或必然的损害,Microsoft 不承担任何责任. 在 Visual Studio

C#之索引器

实际中不使用这个东西,只做了解 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 索引器 { class Program { static void Main(string[] args) { person p = new person(); p[0] = 1; p[1] = 2; p[2] =

C# 索引器

索引器允许累或结构的实例像数组一样进行索引. 当你定义一个索引器时,该类的行为就会像一个虚拟数组,你可以使用数组访问运算符([]) 来访问该类的实例. 以为所引起语法如下: element-type this[int index] { // get 访问器 get { // 返回 index 指定的值 } // set 访问器 set { // 设置 index 指定的值 } } //示例代码如下: class SampleCollection<T> { private T[] arr = n

索引器的使用

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 索引器的使用 { class Program { static void Main(string[] args) { //int[] nums = { 1, 2, 3, 4, 5 }; //Console.WriteLine(nums[2]); P

索引器(C# 编程指南)

索引器(C# 编程指南) Visual Studio 2015 其他版本 索引器允许类或结构的实例就像数组一样进行索引. 索引器类似于属性,不同之处在于它们的取值函数采用参数. 在下面的示例中,定义了一个泛型类,并为其提供了简单的 get 和 set 取值函数方法(作为分配和检索值的方法). Program 类创建了此类的一个实例,用于存储字符串. C# class SampleCollection<T> { // Declare an array to store the data elem

18._4索引器概述及声明

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _18._4索引器概述及声明 { public class Clerk { private string name; public string Name { get { return name; } set { name = value; } }

索引器实现

索引器允许类或者结构的实例按照与数组相同的方式进行索引取值,索引器与属性类似,不同的是索引器的访问是带参的. 索引器和数组比较: (1)索引器的索引值(Index)类型不受限制 (2)索引器允许重载 (3)索引器不是一个变量 索引器和属性的不同点 (1)属性以名称来标识,索引器以函数形式标识 (2)索引器可以被重载,属性不可以 (3)索引器不能声明为static,属性可以 代码实现: 1 //普通索引器 2 public class SimpleClass 3 { 4 string[] arr

《Inside C#》笔记(六) 属性、数组、索引器

一 属性 a) 属性可用于隐藏类的内部成员,对外提供可控的存取接口.属性相当于有些语言的getter.setter方法,只是使用起来更加方便一点,而且查看对应的IL码可以看到,属性的本质也确实是方法. b) 通过只提供get,可以让属性只读.只写属性也可以,但没有用过. c) 属性除了用来控制对类成员的访问外,还可以在get或set的时候通过编码进行一些附加的动作. d) 属性也可以被继承.重写. 二 数组 a) 在C#中,所有数组都继承自System.Array类.数组也是对象,所以声明的数组

C#索引器:在集合或数组中取出某一个元素 举例 _【转】

Garmmar: [访问修饰符] 数据类型 this[参数列表] { get { 获取索引器的内容 } set { 设置索引器的内容 } } Eg: 1 <span style="font-size:14px;">using System; 2 using System.Collections.Generic; 3 using System.Text; 4 5 namespace IndexerUsing 6 { 7 class Photo 8 { 9 10 private