C#索引属性用法实例分析
作者:pythoner 发布时间:2023-02-02 14:15:19
标签: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#程序设计有所帮助。


猜你喜欢
- 本文实例讲述了java数据结构与算法之中缀表达式转为后缀表达式的方法。分享给大家供大家参考,具体如下://stackpublic class
- Spring容器中的Bean是否线程安全,容器本身并没有提供Bean的线程安全策略,因此可以说Spring容器中的Bean本身不具备线程安全
- 这是之前软工课设我写的java访问mysql工具类,它经过了多轮的测试,应该能够适应大多数的操作需求。比之前大二写的更鲁棒,更易用。pack
- 一、国际化准备资源文件,资源文件的命名格式如下:baseName_language_country.propertiesbaseName_l
- ArrayBlockingQueue有界的阻塞队列,内部是一个数组,有边界的意思是:容量是有限的,必须进行初始化,指定它的容量大小,以先进先
- 本文实例讲述了C#使用oledb操作excel文件的方法。分享给大家供大家参考。具体分析如下:不管什么编程语言都会提供操作Excel文件的方
- 一、堆的概念堆的定义:n个元素的序列{k1 , k2 , … , kn}称之为堆,当且仅当满足以下条件时:(1)ki
- 首先利用IDEA创建Maven工程项目1.选择新建项目2.选中Maven骨架3.填写项目名称和项目位置4.Finsh之后默认打开的是pom.
- 代码实例一 using System; using System.IO; using System.Collections; using S
- 本文以一个asp.net程序为例讲述了Repeater中添加按钮实现点击按钮获取某一行数据的方法,分享给大家供大家参考借鉴之用。具体步骤如下
- 本文实例讲述了Java生产者消费者模式。分享给大家供大家参考,具体如下:java的生产者消费者模式,有三个部分组成,一个是生产者,一个是消费
- @Async注解如何实现方法异步处理大批量数据的时候,效率很慢。所以考虑一下使用多线程。刚开始自己手写的一套,用了线程池启动固定的线程数进行
- 一、为什么按值调用和按引用调用?方法或函数可以通过两种方式调用。一种是按值调用,另一种是按引用调用,这两种方式通常根据作为输入或参数传递给它
- 今天是开篇,得要吹一下算法,算法就好比程序开发中的利剑,所到之处,刀起头落。 针对现实中的排序问题,算法有七把利剑可以助你马道成功
- 想必大家都知道,国内的Android应用基本都是免费的
- 本文实例实现一个启动画面,采用了显示Aform,过一段时间,隐藏这个Aform,showdialog下一个Bform,closeAForm这
- 前言链表是一种数据结构,和数组同级。比如,Java中我们使用的ArrayList,其实现原理是数组。而LinkedList的实现原理就是链表
- 上篇文章给大家介绍了浅析C# 中的类型系统(值类型和引用类型),接下来通过本文给大家介绍下c# 泛型类型,说下C#中的泛型,熟练地使用泛型能
- 前言1.因为涉及到对象锁,Wait、Notify一定要在synchronized里面进行使用。2.Wait必须暂定当前正在执行的线程,并释放
- 一、一对一关联 1.1、提出需求根据班级id查询班级信息(带老师的信息)1.2、创建表和数据创建一张教师表和班级表,这里我们假设一