An indexer is a member that enables an object to be indexed in the same way
as an array. Indexers are declared
using indexer-declarations:
indexer-declaration:
attributesopt indexer-modifiersopt indexer-declarator {
accessor-declarations }
indexer-modifiers:
indexer-modifier
indexer-modifiers indexer-modifier
indexer-modifier:
new
public
protected
internal
private
virtual
sealed
override
abstract
extern
indexer-declarator:
type this [ formal-parameter-list ]
type interface-type . this [ formal-parameter-list ]
An indexer-declaration may include a set of attributes (§24) and a valid
combination of the four access modifiers
(§17.2.3), the new (§17.2.2), virtual (§17.5.3), override (§17.5.4),
sealed (§17.5.5), abstract (§17.5.6),
and extern (§17.5.7) modifiers.
Indexer declarations are subject to the same rules as method declarations (§
17.5) with regard to valid
combinations of modifiers, with the one exception being that the static
modifier is not permitted on an indexer
declaration.
The modifiers virtual, override, and abstract are mutually exclusive except
in one case. The abstract
and override modifiers may be used together so that an abstract indexer can
override a virtual one.
C# LANGUAGE SPECIFICATION
252
The type of an indexer declaration specifies the element type of the
indexer introduced by the declaration. Unless
the indexer is an explicit interface member implementation, the type is
followed by the keyword this. For an
explicit interface member implementation, the type is followed by an
interface-type, a ?.?, and the keyword
this. Unlike other members, indexers do not have user-defined names.
The formal-parameter-list specifies the parameters of the indexer. The
formal parameter list of an indexer
corresponds to that of a method (§17.5.1), except that at least one
parameter must be specified, and that the ref
and out parameter modifiers are not permitted.
The type of an indexer and each of the types referenced in the
formal-parameter-list must be at least as accessible
as the indexer itself (§10.5.4).
The accessor-declarations (§17.6.2), which must be enclosed in ?{? and ?}?
tokens, declare the accessors of the
indexer. The accessors specify the executable statements associated with
reading and writing indexer elements.
Even though the syntax for accessing an indexer element is the same as that
for an array element, an indexer
element is not classified as a variable. Thus, it is not possible to pass
an indexer element as a ref or out
argument.
The formal-parameter-list of an indexer defines the signature (§10.6) of
the indexer. Specifically, the signature of
an indexer consists of the number and types of its formal parameters. The
element type and names of the formal
parameters are not part of an indexer?s signature.
The signature of an indexer must differ from the signatures of all other
indexers declared in the same class.
Indexers and properties are very similar in concept, but differ in the
following ways:
? A property is identified by its name, whereas an indexer is identified by
its signature.
? A property is accessed through a simple-name (§14.5.2) or a
member-access (§14.5.4), whereas an indexer
element is accessed through an element-access (§14.5.6.2).
? A property can be a static member, whereas an indexer is always an
instance member.
? A get accessor of a property corresponds to a method with no parameters,
whereas a get accessor of an
indexer corresponds to a method with the same formal parameter list as the
indexer.
? A set accessor of a property corresponds to a method with a single
parameter named value, whereas a set
accessor of an indexer corresponds to a method with the same formal
parameter list as the indexer, plus an
additional parameter named value.
? It is a compile-time error for an indexer accessor to declare a local
variable with the same name as an indexer
parameter.
? In an overriding property declaration, the inherited property is accessed
using the syntax base.P, where P is
the property name. In an overriding indexer declaration, the inherited
indexer is accessed using the syntax
base[E], where E is a comma-separated list of expressions.
Aside from these differences, all rules defined in §17.6.2 and §17.6.3
apply to indexer accessors as well as to
property accessors.
When an indexer declaration includes an extern modifier, the indexer is
said to be an external indexer. Because
an external indexer declaration provides no actual implementation, each of
its accessor-declarations consists of a
semicolon.
[Example: The example below declares a BitArray class that implements an
indexer for accessing the
individual bits in the bit array.
using System;
class BitArray
{
int[] bits;
int length;
Chapter 17 Classes
253
public BitArray(int length) {
if (length < 0) throw new ArgumentException();
bits = new int[((length - 1) >> 5) + 1];
this.length = length;
}
public int Length {
get { return length; }
}
public bool this[int index] {
get {
if (index < 0 || index >= length) {
throw new IndexOutOfRangeException();
}
return (bits[index >> 5] & 1 << index) != 0;
}
set {
if (index < 0 || index >= length) {
throw new IndexOutOfRangeException();
}
if (value) {
bits[index >> 5] |= 1 << index;
}
else {
bits[index >> 5] &= ~(1 << index);
}
}
}
}
An instance of the BitArray class consumes substantially less memory than a
corresponding bool[] (since
each value of the former occupies only one bit instead of the latter?s one
byte), but it permits the same operations
as a bool[].
The following CountPrimes class uses a BitArray and the classical ?sieve?
algorithm to compute the number
of primes between 1 and a given maximum:
class CountPrimes
{
static int Count(int max) {
BitArray flags = new BitArray(max + 1);
int count = 1;
for (int i = 2; i <= max; i++) {
if (!flags[i]) {
for (int j = i * 2; j <= max; j += i) flags[j] = true;
count++;
}
}
return count;
}
static void Main(string[] args) {
int max = int.Parse(args[0]);
int count = Count(max);
Console.WriteLine("Found {0} primes between 1 and {1}", count, max);
}
}
Note that the syntax for accessing elements of the BitArray is precisely
the same as for a bool[]. end
example]
[Example: The following example shows a 26×10 grid class that has an
indexer with two parameters. The first
parameter is required to be an upper- or lowercase letter in the range A?Z,
and the second is required to be an
integer in the range 0?9.
C# LANGUAGE SPECIFICATION
254
using System;
class Grid
{
const int NumRows = 26;
const int NumCols = 10;
int[,] cells = new int[NumRows, NumCols];
public int this[char c, int colm]
{
get {
c = Char.ToUpper(c);
if (c < ’A’ || c > ’Z’) {
throw new ArgumentException();
}
if (colm < 0 || colm >= NumCols) {
throw new IndexOutOfRangeException();
}
return cells[c - ’A’, colm];
}
set {
c = Char.ToUpper(c);
if (c < ’A’ || c > ’Z’) {
throw new ArgumentException();
}
if (colm < 0 || colm >= NumCols) {
throw new IndexOutOfRangeException();
}
cells[c - ’A’, colm] = value;
}
}
}
end example]
17.8.1 Indexer overloading
The indexer overload resolution rules are described in §14.4.2.
分享到:
相关推荐
CSharp - Module 13_Properties and Indexers
Indexers
As you can see,indexers behave much like a custom collection supporting the IEnumeratorand IEnumerableinterfaces in that they provide access to a container’s subitems. The major difference ofcourse ...
CSharp.24索引器 分度器索引 var stringCollection = new SampleCollectionInt (); stringCollection [ 0 ]; dentro de la clase debe tener un array y undice private T [] arr = new T [ 100 ];...
tensorstensorstensors
It can be deployed on top of a variety of storage systems (relational databases, in-memory, filesystems, keyword indexers, etc.), and offers a large scala of tools to developers to leverage the power...
After completing this course, the student will be able to: n List the major elements of the .NET Framew ork and explain how ...n Implement properties and indexers. n Use predefined and custom attributes.
微软内部非公开资料,值得一读。 Objectives After completing this course, you will be able to: List the major elements of ...Implement properties and indexers. Use predefined and custom attributes.
Indexers on classes in assemblies that contain markup are no longer renamed by default. If a Feature Stop cannot be correlated to a Feature Start on the same thread, it is matched with a Feature Start...
Use enumerations, structures, generics, collections, indexers, and other advanced features Create in-memory data queries with LINQ query expressions Improve application throughput and response time ...
ISBN 978-1-78216-152-3 Cover Image by Karl Moore (karl@karlmoore.co.uk) Credits Author Proofreaders Erik Westra Stephen Silk Katherine Tarr Reviewers Will Cadell Indexers Richard Marsden Hemangini ...
Methods, properties, arrays, indexers, and attributes• Interfaces WRITING CODE • Expressions and operators • Program flow control• String handling and regular expressions &#...
16 Using Indexers 315 17 Interrupting Program Flow and Handling Events 329 18 Introducing Generics 353 19 Enumerating Collections 381 20 Querying In-Memory Data by Using Query Expressions 395 21...
袖口 从命令行查询搜索API。 安装 依存关系 如果您是手动安装,则还需要安装以下依赖项: 访问服务器。 手动的 要手动安装cuff只需git clone此存储库并将脚本放置... cuff [OPTIONS] {search, config, indexers, cat
- crawlers, indexers, query engines, and the PageRank algorithm * Optimization algorithms that search millions of possible solutions to a problem and choose the best one * Bayesian filtering, used in ...
与原生 Rust函数和类型紧密集成,包括getter/setter 、methods和indexers 。通过外部Scope自由地将 Rust 变量/常量传递到脚本中——支持所有可克隆的 Rust 类型;无需实现任何特殊特性。内置支持最常见的数据类型,...
1.6.7.3 Indexers 23 1.6.7.4 Events 24 1.6.7.5 Operators 24 1.6.7.6 Destructors 25 1.7 Structs 25 1.8 Arrays 26 1.9 Interfaces 27 1.10 Enums 29 1.11 Delegates 30 1.12 Attributes 31 2. Lexical structure...
9. Arrays, Indexers, and Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Arrays 156 The foreach Statement 162 Indexers 177 Collection Interfaces 186 Constraints...