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
- Difference Between Properties and Indexers in C#
- Overloading in PHP
- C# | Method Overloading
- C# | Operator Overloading
- C# | Constructor Overloading
- Method Overloading In Ruby
- Operator Overloading in Ruby
- Operator Overloading in Python
- Types of Operator Overloading in C++
- Function overloading and Overriding in PHP
- C++ Program to concatenate two strings using Operator Overloading
- How to put the text inside of a created icon?
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.