Current location - Quotes Website - Signature design - What's the difference between indexers and arrays in C#?
What's the difference between indexers and arrays in C#?
Generally speaking, the class that implements the indexer is a collection class or contains a collection class. The indexer allows you to easily reference the data in this collection.

The indexer does not need to be declared, and can be used directly on the class that declares the indexer.

The main difference between indexers and arrays is that

The type of index value of indexer is not necessarily an integer.

The index value used to access the array must be an integer, but the indexer can define other types of index values.

Indexers allow overloading.

A class is not limited to defining only one indexer. As long as the function signatures of indexers are different, a class can have many indexers, and you can overload its functions.

Indexer is not a variable.

The difference between an index and an array is that an indexer does not directly correspond to a reference data store, while an array directly corresponds to a reference data store. Indexers have get accessors and set accessors, which are used to indicate the code to be executed when reading or writing indexer elements.

In your example, there is an int array or collection inside MyClass, and the index you use actually refers to the internal collection class.