Indexer C #

Source: Internet
Author: User
ArticleDirectory
    • Feedback

The indexer is a special class member that allows an object to be accessed in an array-like manner.ProgramIt looks 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 {//
Obtain the attributeCode}

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 <=
0 ))
{
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.

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, and there are no implementation statements. 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. If the set accessors are write-only, 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.

Sexual Cable
Guide

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.

Green Channel: Please follow my favorites to contact me

Feedback

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 <=
0 ))
{
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.

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, and there are no implementation statements. 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. If the set accessors are write-only, 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.

Sexual Cable
Guide

methods can be called, just like public data members

allow calling methods on an object, as if the object is an array

access by simple name

access via indexer

it can be a static or 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

besides the value parameter, its set accessors also have the same table 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.