C # Indexer

Source: Internet
Author: User

[Csharp]
 

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 type index]

{

Get {// code for obtaining attributes}

Set {// set the Property Code}

}


Modifiers include public, protected, private, internal, new, virtual, sealed, override, abstract, and 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:

[Csharp]
/// <Summary>
/// The indexer obtains or sets the value of this position based on the index. It can only store 100
/// </Summary>
Class CIndex
{
// Declare an array that can contain 100 strings
Private string [] array = new string [100];
 
/// <Summary>
/// The indexer obtains or sets the value of this position based on the index.
/// </Summary>
/// <Param name = "index"> </param>
/// <Returns> </returns>
Public string this [int index]
{
Get
{
If (index <0 | index> = 100)
Return string. Empty;
Else
Return array [index];
}
Set
{
If (index> = 0 & index <100)
Array [index] = value;
 
}
}
}

/// <Summary>
/// The indexer obtains or sets the value of this position based on the index. It can only store 100
/// </Summary>
Class CIndex
{
// Declare an array that can contain 100 strings
Private string [] array = new string [100];

/// <Summary>
/// The indexer obtains or sets the value of this position based on the index.
/// </Summary>
/// <Param name = "index"> </param>
/// <Returns> </returns>
Public string this [int index]
{
Get
{
If (index <0 | index> = 100)
Return string. Empty;
Else
Return array [index];
}
Set
{
If (index> = 0 & index <100)
Array [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:


[Csharp]
Static void Main (string [] args)
{
CIndex arr = new CIndex ();
Arr [0] = "My name is ";
Arr [1] = "Jason Divas ";
 
Console. WriteLine (arr [0] + arr [1]);
Console. ReadKey ();
}

Static void Main (string [] args)
{
CIndex arr = new CIndex ();
Arr [0] = "My name is ";
Arr [1] = "Jason Divas ";

Console. WriteLine (arr [0] + arr [1]);
Console. ReadKey ();
}

 

Create an object CIndex first, and then reference the array elements in the object through the index.


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:


[Csharp]
Public interface IAddress
{
String this [int index] {get; set ;}
String Address {get; set ;}
String Answer ();
}

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.
 


 


Author: work201003

Related Article

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.