[C# Tutorial] C# Indexer (Indexer)
Dec 24, 2016 pm 01:21 PMC# Indexer
Indexer allows an object to be indexed like an array. When you define an indexer for a class, the class behaves like a virtual array. You can access instances of this class using the array access operator ([ ]).
Syntax
The syntax of a one-dimensional indexer is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Purpose of Indexer
The declaration of the behavior of an indexer is similar to a property to some extent. Just like properties, you can define indexers using the get and set accessors. However, properties return or set a specific data member, while indexers return or set a specific value of an object instance. In other words, it breaks the instance data into smaller parts and indexes each part, getting or setting each part.
Defining a property includes providing the property name. The indexer is defined without a name, but with the this keyword, which points to the object instance. The following example demonstrates this concept:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
When the above code is compiled and executed, it produces the following results:
1 2 3 4 5 6 7 8 9 10 |
|
Overloading the indexer (Indexer)
The indexer (Indexer) can be overloaded. An indexer can also be declared with multiple parameters, and each parameter can be of a different type. There is no need for the indexer to be integer. C# allows indexers to be of other types, for example, string types.
The following example demonstrates the overloaded indexer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
|
When the above code is compiled and executed, it will produce the following results:
1 2 3 4 5 6 7 8 9 10 11 |
|
The above is the content of [c# tutorial] C# Indexer, more For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
