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

 更新时间:2015年06月28日 10:05:18   作者:pythoner  
这篇文章主要介绍了C#索引属性用法,实例分析了C#声明索引属性的相关技巧,需要的朋友可以参考下

本文实例讲述了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#实现Excel合并单元格数据导入数据集详解

    C#实现Excel合并单元格数据导入数据集详解

    这篇文章主要为大家详细介绍了C#如何实现Excel合并单元格数据导入数据集,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-01-01
  • Winform实现调用asp.net数据接口实例

    Winform实现调用asp.net数据接口实例

    这篇文章主要介绍了Winform实现调用asp.net数据接口的方法,以实例的形式讲述了数据接口及反射辨别响应的实现方法,具有一定的参考借鉴价值,需要的朋友可以参考下
    2014-10-10
  • c#测试本机sql运算速度的代码示例分享

    c#测试本机sql运算速度的代码示例分享

    本文代码目的很简单,就是使用c#测试一下本机sql运算的速度,使用循环往数据里大量插入数据,计算所用时间,大家参考使用吧
    2014-01-01
  • 【C#基础】Substring截取字符串的方法小结(推荐)

    【C#基础】Substring截取字符串的方法小结(推荐)

    这篇文章主要介绍了Substring截取字符串方法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-05-05
  • 详解C#读取Appconfig中自定义的节点

    详解C#读取Appconfig中自定义的节点

    我们往往需要在App.config中自定义一些节来满足实际需要,而不依赖于App.config的appSettings,下面通过一个简单的实例来说明自定义配置节点的设置与读取
    2015-06-06
  • C#在Windows窗体控件实现内容拖放(DragDrop)功能

    C#在Windows窗体控件实现内容拖放(DragDrop)功能

    这篇文章介绍了C#在Windows窗体控件实现内容拖放(DragDrop)的功能,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05
  • C#常用正则大全分享

    C#常用正则大全分享

    C#常用正则大全分享,最基本也是最常用的一些表达式,需要的朋友可以参考一下
    2013-03-03
  • C#实体对象序列化成Json并让字段的首字母小写的两种解决方法

    C#实体对象序列化成Json并让字段的首字母小写的两种解决方法

    这篇文章主要介绍了C#实体对象序列化成Json并让字段的首字母小写的两种方法,在这两种方法中小编比较推荐使用第二种方法,需要的朋友可以参考下
    2018-06-06
  • C#创建Windows服务与服务的安装、卸载

    C#创建Windows服务与服务的安装、卸载

    这篇文章介绍了C#创建Windows服务与服务的安装、卸载,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-02-02
  • C# 中的委托与事件机制详解

    C# 中的委托与事件机制详解

    本文详细介绍了C#中委托和事件的概念、使用方法和应用场景,包括委托定义、匿名方法、Lambda表达式、事件机制和多播委托的详细说明,委托作为类型安全的函数指针,文章还探讨了如何通过匿名方法和Lambda表达式简化委托的使用,以及多播委托在事件处理中的应用
    2024-10-10

最新评论