Open In App

C# - Overloading of Indexers

Last Updated : 04 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C#, Indexer allows an instance of a class or struct to be indexed as an array. When an indexer is defined for a class, then that class will behave like a virtual array. It can be overloaded. C# has 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 similar to method overloading.

Example 1: Overload the Indexer by int and float Data Types

C#
// C# Program to illustrate 
// the overloading of indexers 
using System; 
	
class Geeks { 
	
	// private array of 
	// strings with size 2 
	private string[] word = new string[2]; 
	
	// this indexer gets executed 
	// when Obj[0]gets executed 
	public string this[int flag] { 
		
		// using get accessor 
		get
		{ 
			string temp = word[flag]; 
			return temp; 
		} 
				// using set accessor 
		set
		{ 
			word[flag] = value; 
		} 
	} 
	
	// this is an Overloaded indexer 
	// which will execute when 
	// Obj[1.0f] gets executed 
	public string this[float flag] 
	{ 
		
		// using get accessor 
		get
		{ 
			string temp = word[1]; 
			return temp; 
		} 
		
		// using set accessor 
		set
		{ 		
			// it will set value of 
			// the private string 
			// assigned in main 
			word[1] = value; 			
		} 
	} 
	
static void Main(string[] args) 
{ 
	Geeks Obj = new Geeks(); 
	
	// Value of word[0] 
	Obj[0] = "Hello"; 
	
	// Value of word[1] 
	Obj[1.0f] = " Geeks"; 
	
	Console.WriteLine(Obj[0] + Obj[1.0f]); 
} 
} 

Output
Hello Geeks

Explanation: In the above example, int and float types are used to overload the indexer. The “Hello” word is assigned using the int indexer whereas the float parameter is used to give the value “Geeks” to the string.

Example 2: Overload Indexers by int and string data types and using get accessor (Read-Only).

C#
// Demonstrtion of indexer overloading by taking 
// only get accessor in overloaded indexer in C#
using System;

class Geeks
{
	private string[] str = new string[2];

	// this indexer gets called 
	// when Obj[0] gets executed 
	public string this[int flag]
	{
		// using get accessor 
		get
		{
			string temp = str[flag];
			return temp;
		}
      
		// using set accessor 
		set
		{
			str[flag] = value;
		}
	}

	// this indexer gets called 
	// when Obj["GFG"] gets executed 
	public string this[string flag]
	{

		// using get accessor 
		get
		{   // read only mode 
			return " C# Indexers Overloading.";
		}
	}

	static void Main(string[] args)
	{
		Geeks Obj = new Geeks();

        // Value of str[0] 
		Obj[0] = "This is"; 

		Console.WriteLine(Obj[0] + Obj["GFG"]);
	}
}

Output
This is C# Indexers Overloading.

Explanation: In the above example, we are using only the get accessor in an overloaded indexer that enables the read-only mode. This means we can not modify the given value. Int and string types are used to overload the indexer. Public string this[string flag] also enables read-only mode.

Note: The indexer overloading cannot be done by just changing the return type of the get block.


Similar Reads