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 = ;
int inWord = -;
start = length = ; for (int i = begin; i <= end; ++i)
{
bool isLetter = i < end && Char.IsLetterOrDigit(text[i]); if (inWord >= )
{
if (!isLetter)
{
if (count++ == wordCount)
{
start = inWord;
length = i - inWord;
return true;
}
inWord = -;
}
}
else
{
if (isLetter)
inWord = i;
}
}
return false;
} // 获取和设置包含文档中的字的索引器:
public string this[int index]
{
get
{
int start, length;
if (GetWord(document.TextArray, , index, out start,
out length))
return new string(document.TextArray, start, length);
else
throw new IndexOutOfRangeException();
}
set
{
int start, length;
if (GetWord(document.TextArray, , index, out start,
out length))
{
// 用字符串“value”替换位于 start/length 处的
// 字:
if (length == value.Length)
{
Array.Copy(value.ToCharArray(), ,
document.TextArray, start, length);
}
else
{
char[] newText =
new char[document.TextArray.Length +
value.Length - length];
Array.Copy(document.TextArray, , newText,
, start);
Array.Copy(value.ToCharArray(), , 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 = , start = , length = ;
while (GetWord(document.TextArray, start + length, ,
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 = ; i < d.Words.Count; ++i)
{
if (d.Words[i] == "peter")
d.Words[i] = "penelope";
} // 将字符“p”更改为“P”
for (int i = ; 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/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 |
ylbtech-LanguageSamples-Indexers_2(索引器)的更多相关文章
- ylbtech-LanguageSamples-Indexers(索引器)
ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-Indexers(索引器) 1.A,示例(Sample) 返回顶部 “索引器”示例 本示 ...
- 【.net 深呼吸】细说CodeDom(7):索引器
在开始正题之前,先补充一点前面的内容. 在方法中,如果要引用方法参数,前面的示例中,老周使用的是 CodeVariableReferenceExpression 类,它用于引用变量,也适用于引用方法参 ...
- C# 索引器,实现IEnumerable接口的GetEnumerator()方法
当自定义类需要实现索引时,可以在类中实现索引器. 用Table作为例子,Table由多个Row组成,Row由多个Cell组成, 我们需要实现自定义的table[0],row[0] 索引器定义格式为 [ ...
- C#基础回顾(三)—索引器、委托、反射
一.前言 ------人生路 ...
- C#索引器
索引器允许类或者结构的实例按照与数组相同的方式进行索引取值,索引器与属性类似,不同的是索引器的访问是带参的. 索引器和数组比较: (1)索引器的索引值(Index)类型不受限制 (2)索引器允许重载 ...
- C#之索引器
实际中不使用这个东西,只做了解 using System; using System.Collections.Generic; using System.Linq; using System.Text ...
- C#属性-索引器-里氏替换-多态-虚方法-抽象-接口-泛型-
1.属性 //属性的2种写法 public class person { private string _name; public string Name { get { return _name; ...
- 《精通C#》索引器与重载操作符(11.1-11.2)
1.索引器方法结构大致为<modifier><return type> this [argument list],它可以在接口中定义: 在为接口声明索引器的时候,记住声明只是表 ...
- 描述一下C#中索引器的实现过程,是否只能根据数字进行索引?
不是.可以用任意类型. 索引器是一种特殊的类成员,它能够让对象以类似数组的方式来存取,使程序看起来更为直观,更容易编写. 1.索引器的定义 C#中的类成员可以是任意类型,包括数组和集合.当一个类包含了 ...
随机推荐
- 使用 Visual Studio 部署 .NET Core 应用 ——.Net Core 部署到Ubuntu 16.04
.Net Core 部署到Ubuntu 16.04 中的步骤 1.安装工具 1.apache 2..Net Core(dotnet-sdk-2.0) 3.Supervisor(进程管理工具,目的是服务 ...
- 算法入门系列2:k近邻算法
用官方的话来说,所谓K近邻算法(k-Nearest Neighbor,KNN),即是给定一个训练数据集,对新的输入实例,在训练数据集中找到与该实例最邻近的K个实例(也就是上面所说的K个邻居), 这K个 ...
- MAC Pro 2017款 无线上网慢
MAC Pro 2017款 在无线路由器和MAC相隔一个房间,上网很慢,怀疑是无线路由器有问题,但其他几台老款MAC和PC上网正常.后来将蓝牙关掉,上网就很快了.
- git 查看父分支
// 显示本地分支和服务器分支的映射关系 git branch -vv // 切换分支(和创建分支就差一个-b参数) git checkout {{branch_name}} // 创建新分支,新分支 ...
- hdu 1026(优先队列+路径输出)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- nodejs调试基础【转载】
nodejs调试基础[转载] 看到了一片不错的文章: 作者:前端求生之路 链接:nodejs调试基础[转载]
- Unity做360度的全景照片
这里推荐两种方法,第一种是用鼠标滑动,第二种是用手机的陀螺仪进行全景查看 第一种: 1.新建一Sphere,然后为其赋予材质,注意材质的Shader类型为:Mobile/particles/Alpha ...
- 连接LilyPad之Windows平台的驱动
连接LilyPad之Windows平台的驱动 LilyPad和其他的Arduino控制板的不同之处是它是为电子织物和可穿戴设计的.那么,它的大小就必须要紧凑.所以,它并没有板载其他大多数板子都具有的U ...
- luogu P3375 【模板】KMP字符串匹配
题目描述 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next.如果你不知道这是什么意思也不要问,去百度搜[ ...
- 【数据结构】The Falling Leaves(6-10)
[UVA699]The Falling Leaves 算法入门经典第6章例题6-10(P159) 题目大意:有一颗二叉树,求水平位置的和. 试题分析:乱搞就可以过,将树根节点的pos记为0,向左-1, ...
