C# Interview Questions
C# Programming
Web DevelopmentFrontendBackendGame DevQuestion 25
What are indexers in C#?
Answer:
Indexers in C# allow instances of a class or struct to be indexed just like arrays. This means you can access elements of the object using the array-like syntax. Indexers are useful for creating classes that encapsulate collections or other data structures that support indexing.
Key Concepts of Indexers
-
Index Signature:
- Indexers are defined with the
this
keyword followed by a parameter list inside square brackets. - They can be defined with one or more parameters, though single-parameter indexers are most common.
- Indexers are defined with the
-
Getter and Setter:
- Indexers can have both
get
andset
accessors, similar to properties. - The
get
accessor is used to retrieve values, while theset
accessor is used to assign values.
- Indexers can have both
-
Access Modifiers:
- Indexers can have different access levels for
get
andset
accessors.
- Indexers can have different access levels for
Basic Syntax
public class MyCollection
{
private int[] array = new int[100];
// Define an indexer
public int this[int index]
{
get
{
return array[index];
}
set
{
array[index] = value;
}
}
}
In this example, MyCollection
has an indexer that allows access to its internal array using array-like syntax.
Example Usage
public class Program
{
public static void Main()
{
MyCollection collection = new MyCollection();
collection[0] = 10; // Calls the set accessor
Console.WriteLine(collection[0]); // Calls the get accessor
}
}
public class MyCollection
{
private int[] array = new int[100];
// Define an indexer
public int this[int index]
{
get
{
return array[index];
}
set
{
array[index] = value;
}
}
}
Advanced Example with Multiple Parameters
Indexers can also have multiple parameters:
public class Matrix
{
private int[,] data;
public Matrix(int rows, int columns)
{
data = new int[rows, columns];
}
// Define an indexer with two parameters
public int this[int row, int column]
{
get
{
return data[row, column];
}
set
{
data[row, column] = value;
}
}
}
public class Program
{
public static void Main()
{
Matrix matrix = new Matrix(3, 3);
matrix[0, 0] = 1;
matrix[1, 1] = 2;
matrix[2, 2] = 3;
Console.WriteLine(matrix[0, 0]); // Outputs: 1
Console.WriteLine(matrix[1, 1]); // Outputs: 2
Console.WriteLine(matrix[2, 2]); // Outputs: 3
}
}
Read-Only Indexers
You can create read-only indexers by defining only the get
accessor:
public class ReadOnlyCollection
{
private int[] array = new int[100];
// Define a read-only indexer
public int this[int index]
{
get
{
return array[index];
}
}
}
public class Program
{
public static void Main()
{
ReadOnlyCollection collection = new ReadOnlyCollection();
Console.WriteLine(collection[0]); // Calls the get accessor
}
}
Using Different Data Types for Indexing
Indexers are not limited to integer parameters; you can use other data types as well:
public class StringCollection
{
private Dictionary<string, string> data = new Dictionary<string, string>();
// Define an indexer with a string parameter
public string this[string key]
{
get
{
return data.ContainsKey(key) ? data[key] : null;
}
set
{
data[key] = value;
}
}
}
public class Program
{
public static void Main()
{
StringCollection collection = new StringCollection();
collection["first"] = "Hello";
collection["second"] = "World";
Console.WriteLine(collection["first"]); // Outputs: Hello
Console.WriteLine(collection["second"]); // Outputs: World
}
}
Summary
Indexers in C# provide a way to use array-like syntax for accessing elements within a class or struct. They are defined using the this
keyword and can have one or more parameters. Indexers can have both get
and set
accessors, enabling read and write operations. They are useful for creating more intuitive and flexible classes that encapsulate collections or other indexable data structures. By leveraging indexers, you can enhance the usability and readability of your classes, making them behave more like built-in collections.