Simple understanding and usage of the indexer in C #

Source: Internet
Author: User

The indexer is a special class member that allows an object to be accessed in an array-like manner, making the program 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 arrays and set members, the indexer greatly simplifies access to arrays or set members.

The way to define the indexer is similar to the way to define attributes. The general form is as follows:

[Modifier]Data Type this [Index]

{

Get {//Code for obtaining attributes}

Set {//Code for setting attributes}

}

Modifiers includePublic, protected, Private, internal, new, virtual, sealed, override, abstract, extern.

The data type is the type of the array or set element to be accessed.

The indexer type indicates which type of index the indexer uses to access arrays or set elements. It can be an integer or a string. This indicates operating the array or set members of this object, you can simply understand it as the name of the indexer. Therefore, the indexer cannot have a user-defined name. For example:

Class Z
{
// An integer set that can accommodate 100 Integers
Private long [] arr = new long [100];
// Declare the Indexer
Public long this [int Index]
{
Get
{// Check the 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 be used to access the array members of the class instance. The operation method is similar to that of the array. The general form is as follows:

Object Name [Index]

The data type of the index must be the same as that of the Index Server. For example:

Z = new Z ();

Z [0] = 100;

Z [1] = 101;

Console. writeline (Z [0]);

Creates an Object Z first, and then uses indexes to reference the array elements in the object.

C # does not limit the index type to an integer. For example, you can use a string for the indexer. This type of Indexer can be implemented by searching strings in a set and returning corresponding values. Because the accessors can be overloaded, the string and integer versions can coexist.

Example:

Class daycollection

{

String [] days = {"sun", "mon", "Tues", "wed", "Thurs", "fri", "sat "};

Private int getday (string testday)

{

Int I = 0;

Foreach (string day in days)

{

If (Day = testday)

Return I;

I ++;
}

Return-1;
}

Public int this [String Day]

{

Get {return (getday (day ))}
}
}

 

Static void main (string [] ARGs)

{

Daycollection week = new daycollection ();

Console. writeline ("Fri: {0}", week ["fri"]);

Console. writeline ("ABC: {0}", week ["ABC"]);

}

Result: Fri: 5

ABC:-1

3. Interface Indexer

You can also declare the indexer in the interface. There are two differences between the interface indexer and the class indexer: first, the interface indexer does not use modifiers; second, the interface indexer only contains the accessors get or set.
Statement. The accessors are used to indicate whether the indexer can be read/written, read-only, or write-only. If it can be read/written, the accessors get or set cannot be omitted. If it is read-only, the Set accessors are omitted.
The get accessors are omitted.

For example:

Public interface iaddress

{

String This [int Index] {Get; set ;}

String address {Get; set ;}

String answer ();

}

Indicates that the declared API iaddress contains three members: an indexer, an attribute, and a method. The indexer is readable and writable.

4. Comparison between the indexer and attributes

The indexer and attributes are both class members, which are similar in syntax. The indexer is generally used in custom collection classes. Using the indexer to operate collection objects is as simple as using arrays. attributes can be used in any custom class, it enhances the flexibility of field members of the class.

Gender

 

Call methods are allowed, just like public data members.

The method on the object can be called, as if the object is an array

Access by simple name

Accessible through the Indexer

It can be a static member or an instance Member.

Must be an instance Member

Its get accessors have no parameters.

Its get accessors have the same parameters as the indexer.

Its set accessors include the implicit value parameter.

In addition to the value parameter, its set accessors also have the same parameters as the indexer.

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.