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# | Multidimensional Indexers
- C# | Indexers
- Difference Between Properties and Indexers in C#
- C# - Indexers Using String as an Index
- C++ Program to concatenate two strings using Operator Overloading
- C# | Operator Overloading
- C# | Method Overloading
- Types of Operator Overloading in C++
- C# | Constructor Overloading
- Function overloading and Overriding in PHP
- Operator Overloading in Python
- Overloading in PHP
- Operator Overloading in Ruby
- Method Overloading In Ruby
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.