C # Indexer

Source: Internet
Author: User

Indexer is a new type of class member introduced by C #. It allows objects in the class to be referenced as conveniently and intuitively as arrays. The indexer is very similar to an attribute, but the indexer can have a parameter list and can only act on instance objects, rather than directly act on classes. The class that defines the indexer allows you to use members of the [] OPERATOR category class just like accessing arrays. (Of course, there are still many advanced applications, for example, Arrays can be mapped out through the indexer)

Syntax of The indexer:

1. It can take one or more parameters. 2. Use this as the index name. 3. parameterized member attributes: including the set and get methods. [Access modifier] data type this [data type identifier] {Get {}; set {};} example:
Public class indexsy {private string [] array; Public indexsy (INT num) {array = new string [num]; for (INT I = 0; I <num; I ++) {array [I] = "array" + I;} Public String This [int num] {get {return array [num];} set {array [num] = value ;}}/// indexsy Sy = new indexsy (10); response. write (SY [5]); // output array5

Multi-parameter instances

Public class indexsy {private string [] array; Public indexsy (INT num) {array = new string [num]; for (INT I = 0; I <num; I ++) {array [I] = "array" + I;} Public String This [int num, string con] {get {If (num = 6) {return con ;} else {return array [num]; }}set {If (num = 6) {array [num] = con ;} else {array [num] = value ;}}// method call indexsy Sy = new indexsy (10); Sy [5, "10"] = "Change set value"; response. write (SY [5, ""] + "" + Sy [6, "Change Internal Parameters"] + "" + Sy [8, ""]); // Replace the set value with the internal parameter array8,

  

Comparison between indexer and array:

(1) The index value (INDEX) Type of the indexer is unrestricted.

(2) Reload allowed by the Indexer

(3) The indexer is not a variable.

Differences between indexer and attributes

(1) The attribute is identified by name, and the indexer is identified by function

(2) The indexer can be overloaded and Its Attributes cannot.

(3) The indexer cannot be declared as static, and the attribute can be

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.