Overview of indexers in C #

Source: Internet
Author: User
Tags define get

One of the most interesting parts of the C # language is the indexer of the Class (indexer). In short, an indexer is a special kind of property, through which you can refer to your own class as if you were referencing an array. Obviously, this feature is particularly useful when creating collection classes, and in some other cases, such as handling large files or abstracting certain limited resources, it is certainly useful to have a class with an array-like behavior. This article will lead you to set up classes to adopt indexers. But let's start by outlining the concept of attributes to get the necessary background knowledge.

Property

If you have ever written a program with VB6, then you should be familiar with the attribute method, the so-called attribute method is actually a special class member, which realizes the controlled access to the private class domain. There are two property methods in the C # language, one is get, which allows you to return the value of a private field, or set, by which you can set the value of a private field. For example, in the following code, an FirstName property is created that controls access to private class member FirstName:

class Person {
private string firstname;
public string FirstName {
get {return firstname;}
set {firstname = value;}
}
}
属性声明可以如下编码:
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, the so-called access function (accessor). The Get access function is invoked when the right of an expression invokes a property or a property is used as an argument to another subroutine (or function). Instead, the set access function is invoked when the property is invoked on the left side of the expression and the private domain value is set by implicitly passing the value parameter. You can create a read-only property by omitting the set access function so that any attempt to set the property will result in a compilation error.

Benefits of using indexers

Say for a while we turn to the topic, then why do I have to take this circle? In fact, this is because the indexer of the class is very much like an attribute, as is seen from the code. The following is an example of a class with an indexer that returns a string through the indexer:

class Sample {
public string this [int index] {
get {return "You passed " + index; }
}
}

Note that the property name here is this, meaning the current instance of the callback class, and the argument list is enclosed in square brackets, not parentheses. Also, this is a read-only indexer. To change it to a read/write type, I added a set access function. When you define an indexer, you don't have to take only one parameter. Indexer parameters can take any type, but int is the most commonly used and most reasonable type. You may also have more than one indexer (overload) in the same class.

Once the sample class is defined, we can use the indexer as a default property, as follows:

Sample s = new Sample();
Console.WriteLine(s[55]);

Properties and indexers

There are a number of differences between properties and indexers:

Each attribute of a class must have a unique name, and each indexer defined in the class must have a unique signature (signature) or a list of parameters (so that an indexer overload can be implemented).

The property 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, while the property access function has no parameters.

Interface

Array-like behavior is often loved by program implementations, so you can also define indexers for interfaces, and the IList and IDictionary collection interfaces declare indexers to access their stored items.

When declaring an indexer for an interface, remember that the declaration simply represents the presence of the indexer. You just need to provide the proper access function, and you don't have to include a range modifier. The following code declares the indexer as part of the interface Iimplementme:

interface IImplementMe {
string this[int index]
{
get;
set;
}

The classes that are implemented must specifically define get and set access functions for the Iimplementme indexer.

These are some of the basic overviews of indexers. Now you should have a better understanding of the role of indexers in your development.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.