Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How do I overload the [] operator in C#?
The indexer ([] operator) in C# allows an object to be accessed like an array using square bracket notation. When you define an indexer for a class, instances of that class behave like virtual arrays, enabling array-style access using the [] operator.
Indexers can be overloaded with different parameter types and counts. The parameters don't have to be integers ? you can use strings, multiple parameters, or any other type as the index.
Syntax
Following is the basic syntax for defining an indexer −
public returnType this[indexType index] {
get {
// return the value at index
}
set {
// set the value at index
}
}
For overloaded indexers with different parameter types −
public returnType this[int index] { get; set; }
public returnType this[string key] { get; set; }
public returnType this[int x, int y] { get; set; }
Using Integer Indexer
The most common indexer uses integer parameters to access elements by position −
using System;
class IndexerClass {
private string[] names = new string[10];
public string this[int i] {
get {
return names[i];
}
set {
names[i] = value;
}
}
}
class Program {
static void Main(string[] args) {
IndexerClass Team = new IndexerClass();
Team[0] = "A";
Team[1] = "B";
Team[2] = "C";
Team[3] = "D";
Team[4] = "E";
Team[5] = "F";
Team[6] = "G";
Team[7] = "H";
Team[8] = "I";
Team[9] = "J";
for (int i = 0; i
The output of the above code is −
A
B
C
D
E
F
G
H
I
J
Overloading Indexers with Different Types
You can overload indexers to accept different parameter types. This example shows both integer and string indexers −
using System;
using System.Linq;
class IndexerClass {
private string[] names = new string[10];
public string this[int i] {
get {
return names[i];
}
set {
names[i] = value;
}
}
public string this[string key] {
get {
return names.Where(x => x == key).FirstOrDefault();
}
}
}
class Program {
static void Main(string[] args) {
IndexerClass Team = new IndexerClass();
Team[0] = "A";
Team[1] = "B";
Team[2] = "C";
for (int i = 0; i
The output of the above code is −
A
B
C
Finding 'C': C
Multi-Parameter Indexers
Indexers can accept multiple parameters, useful for matrix-like structures −
using System;
class Matrix {
private int[,] data = new int[3, 3];
public int this[int row, int col] {
get {
return data[row, col];
}
set {
data[row, col] = value;
}
}
}
class Program {
static void Main(string[] args) {
Matrix matrix = new Matrix();
matrix[0, 0] = 1;
matrix[0, 1] = 2;
matrix[1, 0] = 3;
matrix[1, 1] = 4;
Console.WriteLine("Matrix[0,0]: " + matrix[0, 0]);
Console.WriteLine("Matrix[0,1]: " + matrix[0, 1]);
Console.WriteLine("Matrix[1,0]: " + matrix[1, 0]);
Console.WriteLine("Matrix[1,1]: " + matrix[1, 1]);
}
}
The output of the above code is −
Matrix[0,0]: 1
Matrix[0,1]: 2
Matrix[1,0]: 3
Matrix[1,1]: 4
Key Rules for Indexers
-
Indexers must use the
thiskeyword followed by parameters in square brackets. -
Indexers can be overloaded based on the number and type of parameters.
-
Indexers can have
getand/orsetaccessors like properties. -
Read-only indexers have only a
getaccessor.
Conclusion
Indexers in C# provide array-like access to objects using the [] operator. They can be overloaded with different parameter types and counts, making objects more intuitive to use. Indexers are especially useful for collection-like classes and data structures that need positional or key-based access.
