Introduction to C indexer

  • 2020-11-18 06:24:09
  • OfStack

Indexers are special class members that allow objects to be accessed in an array-like manner, making programs more intuitive and easier to write.

1. Definition of indexer

Class members in C# can be of any type, including arrays and collections. When a class contains both array and collection members, the indexer greatly simplifies accessing the array or collection members.

An indexer is defined in a similar way to an attribute, which has the following general form:


[ The modifier ] The data type this[ The index type index] {     get{// Code to get the property }                                                     set{ // Code to set properties } }

Modifiers include public, protected private, internal, new, virtual, sealed, override, abstract, extern.

A data type is the type that represents the array or collection element to be accessed.

The indexer type indicates which type of index the indexer USES to access array or collection elements. It can be integer or string. this represents the array or collection member that operates on this object and can be simply understood as the name of the indexer, so the indexer cannot have a user-defined name. Such as:


class Z
{
        // Can accommodate 100 An integer set of six integers
        private long[] arr = new long[100];
        // Declarative indexer
        public long this[int index]
        {
            get
            { // Check index range
                if (index < 0 || index >= 100)
                {
                    return 0;
                }
                else
                {
                    return arr[index];
                }
            }
            set
            {
                if (!(index < 0 || index >= 100))
                {
                    arr[index] = value;
                }
            }
   }

2. Use of indexer

The indexer can access the array members of an instance of a class. The operation method is similar to the array. The general form is as follows:

Object name [index]

The data type of the index must be the same as the index type of the indexer. Such as:


Z  z=new  z(); z[0]=100; z[1]=101; Console.WriteLine(z[0]);

Means that an object, z, is created before the array elements in that object are referenced by an index.

3. Indexer in the interface

It is also possible to declare an indexer in an interface. There are two differences between an interface indexer and a class indexer: 1. The interface indexer does not use modifiers; 2 is the interface indexer containing only accessors get or set, no implementation statement. The purpose of the accessor is to indicate whether the indexer is read-write, read-only, or write-only, and if it is read-write, neither accessor get nor set can be omitted; If read-only, omit the set accessor; If written only, omit the get accessor.

Such as:


public interface IAddress { string this[int index]{get;set;} string Address{get;set;} string Answer(); }

Indicates that the declared interface, IAddress, contains three members: an indexer, a property, and a method, where the indexer is read-write.

4. Comparison of indexer and attributes

Indexers and attributes are both members of a class and are syntactically very similar. Indexer 1 is commonly used in custom collection classes. Using indexer to manipulate collection objects is as simple as using array 1. Properties can be used for any custom class, which enhances the flexibility of the field members of the class.

Generic cable primer

 允许调用方法,如同公共数据成员

允许调用对象上的方法,如同对象是1个数组

 可通过简单的名称进行访问

可通过索引器进行访问

 可以为静态成员或实例成员

必须为实例成员

 其get访问器没有参数

其get访问器具有与索引器相同的形参表

 其set访问器包含隐式value参数

除了value参数外,其set访问器还具有与索引器相同的形参表


Related articles: