The indexer in C # is newly added, which is somewhat different from the attribute. In C #, the attribute can be as follows:
Class person {
Private string firstname;
Public String firstname
{
Get {return firstname ;}
Set {firstname = value ;}
}
}
The attribute declaration can be encoded as follows:
Person P = new person ();
P. firstname = "Tom ";
Console. writeline (P. firstname );
Attribute declaration is more like a domain Declaration, but it also declares two special members. According to Microsoft, it is called an access function (accessor ). When the call attribute or attribute on the right of an expression is used as another sub-expressionProgramOr a function. If the attribute is called on the left of the expression and the value parameter is implicitly passed to set the private domain value, the Set access function is called.
Indexer is a new type of class member introduced by C #. It allows objects to be referenced as conveniently and intuitively as arrays. The indexer is very similar to the attribute we mentioned earlier. However, the indexer can have a list of parameters and can only act on instance objects, rather than classes. The following is an example:
Using system;
Namespace indexerexample
{
Class mypreviusexp
{
Private string [] mycompanies = new string [10];
// index creation
Public String This [int Index]
{< br>
Get
{< br> If (index <0 or index> = 6)
return "null";
else
return mycompanies [Index];
}< br> set
{< br> If (! (Index <0 or index> = 10)
mycompanies [Index] = value;
}< br>
}< BR >}< br> class mymainclass
{< br> Public static void main ()
{< br> mypreviusexp indexerobj = new mypreviusexp ();
indexerobj [0] = "AMS"
indexerobj [3] = "HCl"
indexerobj [5] = "ACC"
(INT I = 0; I <10; I ++
{< br>
console. writeline ("My companies {0 }:{ 1}", I, indexerobj [I]);
}< br>
}< BR >}
}
We can see that we access the elements in the array, and
Mypreviusexp indexerobj = new mypreviusexp ();
After creating the indexer
Indexerobj [0] = "AMS"
Indexerobj [3] = "HCl"
Indexerobj [5] = "ACC"
Set Value
The final output is:
Mycompanies 0: AMS
Mycompanies 1:
Mycompanies 2:
Mycompanies 3: HCl
Mycompanies 4:
Mycompanies 5: ACC
Mycompanies 6: NULL
Mycompanies 7: NULL
Mycompanies 8: NULL
Mycompanies 9: NULL
Instructions:
C # the most interesting part of the language is the class indexer ). To put it simply, the so-called indexer is a special type of attributes. With these attributes, You can reference your own classes as you reference arrays. Obviously, this function is particularly useful when creating collection classes. In some other cases, such as processing large files or abstracting some limited resources, it is of course very useful to make classes behave like arrays. This article will guide you to set the class to use the indexer. However, first, let's give an overview of the attribute concept to understand the necessary background knowledge.
Attribute
If you have used VB6 to write a program, you should be familiar with the attribute method. The attribute method is actually a special class member and implements controlled access to private class domains. There are two attribute methods in C #. One is get, which can return the value of the private domain, and the other is set, through which the value of the private domain can be set. For exampleCodeFor example, a firstname attribute is created to control the access to the private class member firstname.
Class person {
Private string firstname; Public String firstname
{Get {return firstname ;}
Set {firstname = value ;}
}
}
The attribute declaration 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, but it also declares two special members. According to Microsoft, it is called an access function (accessor ). When the call attribute or attribute on the right of an expression is used as a parameter of another subroutine (or function), The get access function is called. If the attribute is called on the left of the expression and the value parameter is implicitly passed to set the private domain value, the Set access function is called. You can create a read-only attribute by omitting the set access function, so that any attempt to set the attribute will produce a compilation error.
Benefits of using the Indexer
After talking about it for a long time, let's turn to the subject. Why should I go into this circle? In fact, this is because the class indexer is very similar to the attribute, and the Code also looks like this. The following is a class example with an indexer. The indexer returns a string.
Class sample {
Public String This [int Index]
{Get {return "You passed" + index;
}
}
}
Note that the attribute name here is this, which indicates the current instance of the reference class. The parameter list is included in square brackets rather than brackets. Also, this is a read-only index. To change it to the read/write type, I added a set access function. When defining the index, you do not have to use only one parameter. The indexer parameter can use any type, but int is usually the most reasonable type. The same class may also have more than one indexer (reload ). After the sample class is defined as above, we can use the indexer as a default attribute, as shown below:
Sample S = new sample (); console. writeline (s [55]);
Attribute and Indexer
There are some differences between attributes and Indexer
Each attribute of the class must have a unique name, and each indexer defined in the class must have a unique signature) or the parameter list (so that the indexer can be reloaded ).
The attribute can be static, but the indexer must be an instance Member.
The parameters that can be accessed and passed to the indexer by the access function defined for the indexer, while the attribute access function has no parameters.
Interface
Similar arrays are often favored by the program implementers. Therefore, you can also define the indexer for the interface. The ilist and idictionary collection interfaces both declare the indexer to access the projects it stores.
When declaring the indexer for an interface, remember that the declaration only indicates the existence of the indexer. You only need to provide the appropriate access function, and do not need to include the range modifier. The following Code declares the indexer as part of interface iimplementme {string this [int Index] {Get; set ;}
The classes to be implemented must define the get and set access functions for the iimplementme index tool bodies.
The above is a basic overview of the indexer. Now you should have a deep understanding of the role of the indexer in your development.