The indexer in C#(C#中的索引器)
2009-10-29 00:27
465 查看
Indexers allow instances of a class or struct to be indexed just like arrays. Indexers resemble properties except that their accessors take parameters.
In the following example, a generic class is defined and provided with simple get and set accessor methods as a means of assigning and retrieving values. The Program class creates an instance of this class for storing strings.
class SampleCollection<T>
// This class shows how client code uses the indexer.
class Program
//declaration:
class Mlass
//usage:
Mlass m = new Mlass();
m[2, 2] = 3;
Console.WriteLine(m[2, 2]);
Addition
1. Just like any other class members, indexers can also participate in inheritance. A base class indexer is inherited to the derived class.
2. A Base class indexer can be polymorphicaly overridden in a Derived class. But remember that the modifiers like virtual, override etc are using at property level, not at accessor level.
3. An indexer inside a class can be declared as abstract by using the keyword abstract. Remember that an abstract indexer in a class carries no code at all. The get/set accessors are simply represented with a semicolon. In the derived class we must implement both set and get assessors.
If the abstract class contains only set accessor, we can implement only set in the derived class.
Go to my home for more posts
In the following example, a generic class is defined and provided with simple get and set accessor methods as a means of assigning and retrieving values. The Program class creates an instance of this class for storing strings.
class SampleCollection<T>
// This class shows how client code uses the indexer.
class Program
//declaration:
class Mlass
//usage:
Mlass m = new Mlass();
m[2, 2] = 3;
Console.WriteLine(m[2, 2]);
Addition
1. Just like any other class members, indexers can also participate in inheritance. A base class indexer is inherited to the derived class.
2. A Base class indexer can be polymorphicaly overridden in a Derived class. But remember that the modifiers like virtual, override etc are using at property level, not at accessor level.
3. An indexer inside a class can be declared as abstract by using the keyword abstract. Remember that an abstract indexer in a class carries no code at all. The get/set accessors are simply represented with a semicolon. In the derived class we must implement both set and get assessors.
If the abstract class contains only set accessor, we can implement only set in the derived class.
Go to my home for more posts
相关文章推荐
- the Indexer(索引器) in C#
- LINQ: The Future of Data Access in C# 3.0
- What ’ s in the .NET Framework?[摘自Beginning Visual C# 2008]
- Implementing the Singleton Pattern in C#
- The method below converts an array of objects to a DataTable object in C#.
- Creating a Web Control with an Expandable Property in the Designer by Using C#.
- The .NET weak event pattern in C#
- Implementing the Singleton Pattern in C#
- EF 6.0 The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. c#中的时间
- C#梳理【索引器Indexer】
- Book Contents Reviews Notes Errata Articles Talks Downloads Resources Code Formatter Cover of C# in Depth Order now (3rd edition) Implementing the Singleton Pattern in C#
- 【转】单体模式-经典实现 Implementing the Singleton Pattern in C#
- [CLR via C#]1#The CLR's Execution Model WORK-IN-PROGRESS
- Implementing the Singleton Pattern in C#
- Implementing the Singleton Pattern in C#
- Implementing the Singleton Pattern in C#
- New C# Features in the .NET Framework 4
- The implementation of anonymous methods in C# and its consequences
- The Repository Pattern (库模式)Example in C#[引文]
- A full-duplex audio player in C# using the waveIn/waveOut APIs