Prerequisite: Indexers in C#
Like functions, Indexers can also be overloaded. In C#, we can have multiple indexers in a single class. To overload an indexer, declare it with multiple parameters and each parameter should have a different data type. Indexers are overloaded by passing 2 different types of parameters. It is quite similar to method overloading.
Example 1: In the below program int and float types are used to overload the indexer. Here, “Hello” word is assigned using the int indexer whereas the float parameter is used to give the value “Geeks” to the string.
Example 2: In the below program, we are using only get accessor in overloaded indexer which enables the read-only mode. Means we can’t modify the given value. Here int and string types are used to overload the indexer. public string this[string flag] contain only get accessor which enables the read-only mode.
This is C# Indexers Overloading.
Note: The indexer overloading cannot be done by just changing the return type of the get block.
- C# | Indexers
- C# | Multidimensional Indexers
- Overloading in PHP
- C# | Constructor Overloading
- C# | Operator Overloading
- C# | Method Overloading
- Operator Overloading in Ruby
- Function overloading and Overriding in PHP
- Operator Overloading in Python
- Types of Operator Overloading in C++
- C++ Program to concatenate two strings using Operator Overloading
- HashMap computeIfPresent(key, BiFunction) method in Java with Examples
- BlockingQueue remainingCapacity() method in Java with examples
- HashMap replaceAll(BiFunction) method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.