In depth understanding of C indexer of a property to property comparison that supports parameters

  • 2020-05-12 03:08:22
  • OfStack

Indexer is a special class member that allows objects to be accessed in an array-like manner, making programs more intuitive and easier to write.
1. Definition of indexer
The 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 access to the array or collection members.
Indexer //this is an array or collection member that operates on the object. It can be understood simply as the name of the indexer, so remember to differentiate by parameter when of the same type.

//[ The modifier ]  The data type  this[ The index type  index]
{
    get{// The code that gets the property }  
    set{ // The code that sets the properties }
}

Such as

public int this [int index]
{
    get{}
    set{}
}

Specific examples are as follows:

class Z
{
        // Can accommodate 100 The set of integers 
        private long[] arr = new long[100];
        // Declare 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 <= 0))
                {
                    arr[index] = value;
                }
            }
   } 

2. Use of indexer
An indexer can be used to access the array members of an instance of a class. The operation method is similar to that of an array. The form of 1 is as follows:
Object name [index]
Modifiers include public, protected private, internal, new, virtual, sealed, override, abstract, extern, index of the data type must be the same as the indexer's index type. Such as:

Z  z=new  z();
z[0]=100;
z[1]=101;
Console.WriteLine(z[0]);// Means create first 1 An object z , and reference the array elements in the object through the index. 

3. Indexer in the interface
An indexer can also be declared in an interface. There are two differences between an interface indexer and a class indexer: 1. The interface indexer does not use modifiers; 2. 2 is an interface indexer that contains only accessors get or set, with no implementation statements. The purpose of an accessor is to indicate whether the indexer is read-write, read-only, or write-only. If it is read-write, the accessor get or set cannot be omitted. If read-only, omit the set accessor; If you are writing only, omit the get accessor.
Such as:

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

Representing the declared interface IAddress contains three members: an indexer, a property, and a method, where the indexer is read-write.
4. Indexer and attribute comparison
Indexers and properties are both members of a class and are syntactically very similar. Indexer 1 is used in custom collection classes. Using indexer to manipulate collection objects is as simple as using array 1. While properties can be used for any custom class, they enhance the flexibility of the field members of the class.

attribute The indexer Allows methods to be invoked as public data members Allows methods on an object to be called as if the object were an array It can be accessed by a simple name It can be accessed through an indexer It can be a static member or an instance member Must be an instance member Its get accessor has no parameters Its get access appliance has the same parameter list as the indexer Its set visitor contains the implicit value parameter In addition to the value parameter, the set visitor has the same parameter table as the indexer

Related articles: