Overview of the indexer (reproduced) in C #

xiaoxiao2021-03-06  110

C # Language One of the most interesting places is the class of indexer. Simply put, the so-called indexer is a special attribute. You can reference your own class like a reference array. Obviously, this feature is especially useful in creating a collection class, and in some other cases, such as handling large files or abstract limited resources, etc., it is also very useful to make the class with similar arrays. This article will lead you to set the class to use the indexer. However, first let us outline the concept of property to understand the necessary background knowledge. Attributes, if you have written processes in VB6, you should be familiar with the property method. The so-called attribute method is actually a special member, which implements controlled access to private domains. There are two attribute methods in the C # language, one is Get, which can return to the value of the private domain, and the second is SET, and it can set the value of the private domain by it. For example, the following code is an example, and a firstname property is created, which controls access to private member firstname: Class Person {Private String firstname; public string firstname {get {return firstname;} set {firstname = Value }}}

Attribute 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 statement, but it also declares that two special members, according to Microsoft's statement is the so-called access function (Accessor). The GET access function is called when the right call attribute or attribute of a certain expression is used as a parameter for other subroutines (or functions). Conversely, the SET access function will be called when the expression is called the attribute and the private domain value is set by implicitly transmitting the Value parameter. You can create read-only properties, the method is omitted to the SET access function, so that any attempt to set attributes generate compilation errors.

The benefits of using the indexer have been transferred to the topic for a long time, so why do I want to drive this circle? In fact, this is because the indexer of the class is very like attributes, and it is true from the code. The following is a class example with an indexer, and a string will be returned by the indexer:

Class Sample {public string this [int index] {get {return "you passed" index;}}}

Note that the attribute name here is this, which means that the current instance of the citations, the parameter list is included in square brackets inside. Also, this is a read-only indexer. In order to change it into a read / write type, I added a SET access function. When you define an indexer, you don't necessarily use only one parameter. Indexer parameters can be used in any type, but int is usually used is also the most reasonable type. The same category may also have more than one indexer (overload).

After the Sample class is defined above, we can use the indexer as a default attribute as follows:

Sample s = new sample (); console.writeline (s [55]);

There are some differences between attributes and indexer 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 a list of parameter (this can be reloaded). The attribute can be static (static) and the indexer must be an instance member. The access function defined for the indexer can access the parameters passed to the indexer, and the attribute access function is not a parameter. The interface similar to array is often loved by the program implementation, so you can also define an indexer for interfaces, ILIST and IDICTIONARY set interfaces declare an indexer to access projects they store.

When declaring the indexer for the interface, remember that the statement is only the existence of the indexer. You only need to provide the right access function, do not include a range modifier. The following code declares the indexer as part of the interface IIMPLEME:

Interface IIMPLEME {String this [int index] {get; set;} The class of the corresponding implementation must define the GET and SET access functions for the IIMPLEME's indexer.

The above is some basic overview of the indexer. Now you should have a deeper understanding of the role of the indexer in your development.

转载请注明原文地址:https://www.9cbs.com/read-97352.html

New Post(0)