Comparison between the indexer (a property supporting parameters) and the property 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: The indexer // this indicates to operate on the array or set members of the object, you can simply understand it as the name of the indexer. Therefore, when it is of the same type, remember to differentiate it by parameters.

[Html] // [modifier] data type this [index type index]
{
Get {// code for obtaining attributes}
Set {// set the Property Code}
}

// [Modifier] data type this [index type index]
{
Get {// code for obtaining attributes}
Set {// set the Property Code}
} Example
[Html] public int this [int index]
{
Get {}
Set {}
}

Public int this [int index]
{
Get {}
Set {}
} Example:


[Html] 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;
}
}
}

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 the 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]
Modifiers include public, protected, private, internal, new, virtual, sealed, override, abstract, and extern. The index data type must be the same as the index type of The indexer. For example:

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

Z z = new z ();
Z [0] = 100;
Z [1] = 101;
Console. WriteLine (z [0]); // indicates to create an object z 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:

[Html] 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.

Attribute Indexer
A method can be called, just as a public data member can call a method on an object, as if an object is an array
Access by simple name can be accessed by 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.


 

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.