The c indexer details the example

  • 2020-06-15 10:07:10
  • OfStack

1. Definition of indexer

Class members in C# can be of any type, including arrays and collections. When a class contains 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:

Data type this[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, either 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]);

Creates an object, z, and then references the array elements in that object by 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. 2 is an interface indexer that contains only accessors get or set, with no implementation statements. The purpose of the accessor is to indicate whether the indexer is read-write, read-only, or writing-only. 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 used in custom collection classes. Manipulating collection objects using indexer is as simple as using array 1. Properties can be used for any custom class, which enhances the flexibility of the class's field members.

The sample


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace suoyin
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] values = { 3,5,7,9};
            int i = values[1];

            Person p1 = new Person();
            p1[1] = " Xiao Ming ";
            Console.WriteLine(p1[1]+p1[2]);

            Console.WriteLine(p1["tom",3,9]);// Indexes can also be overloaded. 
            Console.ReadKey();
        }
    }

    class Person
    {
        private string FirstName=" Heavy hair ";
        private string SecondName="2 MAO ";

        public string this[string name, int x, int y]
        {
            get
            {
                return name + x + y;
            }
        }
        public string this[int index]// The index is in brackets. 
        {
            get
            {
                if (index == 1)
                {
                    return FirstName;
                }
                else if (index == 2)
                {
                    return SecondName;
                }
                else
                {
                    throw new Exception(" This is the wrong number, dear! ");
                }
            }
            set
            {
                if (index == 1)
                {
                    FirstName = value;
                }
                else if (index == 2)
                {
                    SecondName = value;
                }
                else
                {
                    throw new Exception(" This is the wrong number, dear! ");
                }
            }
        }
    }
}

Related articles: