-
Collection of the implementation process of indexer in C#
One of the most interesting aspects of the C# language is the class indexer. To put it simply, the so-called indexer is a special type of attribute through which you can reference your own class just like an array. Obviously, this feature is particularly useful when creating collection classes, but in other situations, such as processing large files or abstracting certain limited resources, it is of course also very useful to have array-like behavior in a class. This article will walk you through setting up a class to use an indexer. But first, let's outline the concept of properties to provide some necessary background.
Properties If you have ever written a program in VB6, then you should be very familiar with property methods. The so-called property methods are actually special class members that implement controlled access to private class fields. There are two attribute methods in the C# language. One is get, through which the value of the private field can be returned, and the second is set, through which the value of the private field can be set. For example, taking the following code as an example, a FirstName property is created to control access to the private class member firstname:
class Person {
private string firstname;
public string FirstName {
get {return firstname;}
set {firstname = value;}
}
}
Property declarations can be encoded as follows:
Person p = new Person();
p.FirstName = "Lamont";
Console.WriteLine (p.FirstName);
As you can see, the attribute declaration is more like a domain declaration, except that it also declares two special members, which according to Microsoft are so-called accessors. The get access function is called when a property is called on the right side of an expression or when the property is used as a parameter of another subroutine (or function). Conversely, the set access function is called when the property is called on the left side of the expression and the private field value is set by implicitly passing the value parameter. You can create read-only properties by omitting the set accessor function, so that any attempt to set the property will generate a compilation error.
Benefits of using indexers
Below is its structure
<modifier> <return type> this [argument list]
...{
get
...{
// Get codes goes here
}
set
...{
// Set codes goes here
}
}
Note:
modifier: modifier, such as private, public, protected or internal
this: This is a special keyword in C#, which represents the current instance of the reference class. Here it means the index of the current class.
argument list: This refers to the parameters of the indexer.
After talking for a long time, let’s turn to the topic, so why do I have to go around in this circle? In fact, this is because the indexer of a class is very much like a property, and this is also the case from the code point of view. Here is an example of a class with an indexer that returns a string:
class Sample {
public string this [int index] {
get {return "You passed " + index; }
}
}
Note that the attribute name here is this, which refers back to the current instance of the class, and the parameter list is enclosed in square brackets instead of parentheses. Also, this is a read-only indexer. In order to change it to read/write type, I added a set access function. When defining an indexer, you don't necessarily take only one parameter. Indexer parameters can be of any type, but int is usually the most reasonable type. It is also possible to have more than one indexer (overloaded) in the same class.
After defining the Sample class as above, we can use the indexer as a default attribute, as shown below:
Sample s = new Sample();
Console.WriteLine(s[55]);
Properties and Indexers There are several differences between properties and indexers:
Each attribute of the class must have a unique name, and each indexer defined in the class must have a unique signature or parameter list (so that indexer overloading can be implemented).
Properties can be static and indexers must be instance members.
Access functions defined for indexers have access to the parameters passed to the indexer, while property access functions have no parameters.
The array-like behavior of interfaces is often favored by program implementers, so you can also define indexers for the interface. Both the IList and IDictionary collection interfaces declare indexers to access their stored items.
When declaring an indexer for an interface, remember that the declaration only indicates the existence of the indexer. You only need to provide the appropriate access functions, without including scope modifiers. The following code declares the indexer as part of the interface IImplementMe:
interface IIimplementMe {
string this[int index]
{
get;
set;
}
The corresponding implemented class must specifically define the get and set access functions for the indexer of IimplementMe.
That’s a basic overview of indexers. You should now have a better understanding of the role indexers play in your development.
class SampleCollection<T>
{
private T[] arr = new T[100];
public T this[int i]
{
get
{
return arr[i];
}
set
{
arr[i] = value;
}
}
}
// This class shows how client code uses the indexer
class Program
{
static void Main(string[] args)
{
SampleCollection<string> stringCollection = new SampleCollection<string>();
stringCollection[0] = "Hello, World";
System.Console.WriteLine(stringCollection[0]);
}
}
This article comes from the CSDN blog. Please indicate the source when reprinting: http://blog.csdn.net/licheng19891020/archive/2009/12/09/4974516.aspx
-