C#索引器与索引属性

(一)索引器
教程
定义“索引器”使您可以创建作为“虚拟数组”的类。该类的实例可以使用 [] 数组访问运算符进行访问。在 C# 中定义索引器类似于在 C++ 中定义运算符 [],但前者灵活得多。对于封装类似数组的功能或类似集合的功能的类,使用索引器使该类的用户可以使用数组语法访问该类。
例如,假定您想定义一个类,该类使文件显示为字节数组。如果文件非常大,则将整个文件读入内存是不切实际的,尤其在您只想读取或更改少数字节时。通过定义 FileByteArray 类,您可使文件外观类似于字节数组,但读或写字节时,实际执行的是文件的输入和输出。
除下面的示例以外,本教程中还讨论有关“创建索引属性”的高级主题。
示例
本示例中,FileByteArray 类使得像字节数组那样访问文件成为可能。Reverse 类反转文件的字节。可以运行该程序以反转任何文本文件的字节,包括程序源文件本身。若要将反转的文件更改回正常状态,请在同一文件上再次运行该程序。
// indexer.cs
// arguments:
using System;
using System.IO;
// Class to provide access to a large file
// as if it were a byte array.
public class FileByteArray
{
Stream stream;      // Holds the underlying stream
// used to access the file.
// Create a new FileByteArray encapsulating a particular file.
public FileByteArray(string fileName)
{
stream = new FileStream(fileName, FileMode.Open);
}
// Close the stream. This should be the last thing done
// when you are finished.
public void Close()
{
stream.Close();
stream = null;
}
// Indexer to provide read/write access to the file.
public byte this[long index]  // long is a 64-bit integer
{
// Read one byte at offset index and return it.
get
{
byte[] buffer = new byte[1];
stream.Seek(index, SeekOrigin.Begin);
stream.Read(buffer, 0, 1);
return buffer[0];
}
// Write one byte at offset index and return it.
set
{
byte[] buffer = new byte[1] {value};
stream.Seek(index, SeekOrigin.Begin);
stream.Write(buffer, 0, 1);
}
}
// Get the total length of the file.
public long Length
{
get
{
return stream.Seek(0, SeekOrigin.End);
}
}
}
// Demonstrate the FileByteArray class.
// Reverses the bytes in a file.
public class Reverse
{
public static void Main(String[] args)
{
/
/ Check for arguments.
if (args.Length == 0)
{
Console.WriteLine("indexer <filename>");
return;
}
FileByteArray file = new FileByteArray(args[0]);
long len = file.Length;
// Swap bytes in the file to reverse it.
for (long i = 0; i < len / 2; ++i)
{
byte t;
// Note that indexing the "file" variable invokes the            // indexer on the FileByteStream class, which reads            // and writes the bytes in the file.
t = file[i];
file[i] = file[len - i - 1];
file[len - i - 1] = t;
}
file.Close();
}
}
输入:
若要测试程序,可使用具有以下内容的文本文件(该文件在“索引器”示例中称为 )。public class Hello1
{
public static void Main()
{
System.Console.WriteLine("Hello, World!");
}
}
若要反转该文件的字节,请编译程序,然后使用下面的命令行:
若要显示反转的文件,请输入命令:
示例输出
}
}
;)"!dlroW ,olleH"(syS
{
)(niaM diov citats cilbup
{
1olleH ssalc cilbup
代码讨论
* 由于索引器是使用 [] 运算符进行访问的,因此没有名称。有关索引器声明语法,请参见索引器。
* 在上面的示例中,索引器类型是 byte,并采用 long(64 位整数)类型的单个索引。“获取”(Get) 访
问器定义从文件读取一个字节的代码,而“设置”(Set) 访问器定义向文件写入一个字节的代码。在“设置”(Set) 访问器内,预定义的参数值为正赋给虚拟数组元素的值。
* 索引器必须至少有一个参数。尽管相当少见,但索引器可以有多个参数,以模拟多维“虚拟数组”。尽管整数参数最常见,但索引器参数可以为任何类型。例如,标准的“字典”(Dictionary) 类提供参数类型为 Object 的索引器。
* 尽管索引器功能强大,但有一点很重要,仅当类似数组的抽象化有意义时才使用索引器。始终应仔细考虑使用常规方法是否会同样清楚。例如,下面是使用索引器不当的例子:class Employee
{
// VERY BAD STYLE: using an indexer to access
// the salary of an employee.
public double this[int year]
{
get
{
// return employee's salary for a given year.
}
}
}
尽管合法,但只有“获取”(Get) 访问器的索引器通常不是很好的结构。在此情况下,强烈建议考虑使用方法。
(二)索引属性
本教程展示如何实现使用索引属性的类。索引属性使您可以使用表示类似于数组的若干种不同事物的集合的类。学习本教程以前应完成索引器教程。
教程
假定您要编写一个 Document 类,该类封装非常长的文本章节。为能够方便地实现各种操作(如检查
拼写),您可能希望以单词(以及字符)的虚拟数组形式查看文档。
下面的示例展示实现这种类的技术。对于每个“索引属性”,您定义一个嵌套类,该类包含对主类实例的反向引用。主类上的 readonly 字段提供对嵌套类(定义每个虚拟数组)的实例的访问。每个嵌套类定义一个索引器以及其他类似集合的方法(例如 Count 属性)。下面的示例针对“Words”和“Characters”展示这一点。
注意:请慎重使用该技术!仅在使用数组索引操作提供的抽象化能明确阐明使用您的类的代码,并且索引器同时具有“获取”(Get) 和“设置”(Set) 访问器时,才使用该模式。
示例
本示例中定义了 Document 类。使用 Words 和 Characters 这两个索引属性在 Document 对象上执行某些文本操作。
// indexedproperty.cs
using System;
public class Document
{
// Type allowing the document to be viewed like an array of words:
public class WordCollection
{
readonly Document document;  // The containing document
internal WordCollection(Document d)
{
document = d;
}
// Helper function -- search character array "text", starting at
// character "begin", for word number "wordCount." Returns false
/
/ if there are less than wordCount words. Sets "start" and
// length" to the position and length of the word within text:
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;
}
/
/ Indexer to get and set words of the containing document:
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))
{
// Replace the word at start/length with the
// string "value":
if (length == value.Length)

本文发布于:2024-09-22 01:35:10,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/2/88401.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:数组   使用   文件   字节   访问   类似
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议