C#索引属性用法实例分析

所属分类: 软件编程 / C#教程 阅读数: 81
收藏 0 赞 0 分享

本文实例讲述了C#索引属性的用法。分享给大家供大家参考。具体如下:

这里演示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#程序设计有所帮助。

更多精彩内容其他人还在看

c#开发word批量转pdf源码分享

已经安装有Office环境,借助一些简单的代码即可实现批量Word转PDF,看下面的实例源码吧
收藏 0 赞 0 分享

c# xml API操作的小例子

这篇文章主要介绍了c# xml API操作的小例子,有需要的朋友可以参考一下
收藏 0 赞 0 分享

c#唯一值渲染实例代码

这篇文章主要介绍了c#唯一值渲染实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

淘宝IP地址库采集器c#代码

这篇文章主要介绍了淘宝IP地址库采集器c#代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C#在后台运行操作(BackgroundWorker用法)示例分享

BackgroundWorker类允许在单独的专用线程上运行操作。如果需要能进行响应的用户界面,而且面临与这类操作相关的长时间延迟,则可以使用BackgroundWorker类方便地解决问题,下面看示例
收藏 0 赞 0 分享

c#文本加密程序代码示例

这是一个加密软件,但只限于文本加密,加了窗口控件的滑动效果,详细看下面的代码
收藏 0 赞 0 分享

c#生成站点地图(SiteMapPath)文件示例程序

这篇文章主要介绍了c#生成站点地图(SiteMapPath)文件的示例,大家参考使用
收藏 0 赞 0 分享

C# 键盘Enter键取代Tab键实现代码

这篇文章主要介绍了C# 键盘Enter键取代Tab键实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C# WinForm导出Excel方法介绍

在.NET应用中,导出Excel是很常见的需求,导出Excel报表大致有以下三种方式:Office PIA,文件流和NPOI开源库,本文只介绍前两种方式
收藏 0 赞 0 分享

C#串口通信程序实例详解

在.NET平台下创建C#串口通信程序,.NET 2.0提供了串口通信的功能,其命名空间是System.IO.Ports,创建C#串口通信程序的具体实现是如何的呢?让我们开始吧
收藏 0 赞 0 分享
查看更多