How to Use Indexer in Classes and Interfaces of C# (C Sharp)

Does something come to your mind when you read the word indexer? Hope you are recalling index of array elements at this moment. This indexer is not referring to index of array element but it is used to treat a class or interface or struct as an array. This article will guide on understanding the concept of indexers and how to use indexer in classes and interfaces.

Indexers can be declared using the following syntax:
modifier type this[type variableName] {
get { }
set { }

Here are the basic guidelines to use Indexers:

• Indexers do not have a name. However if you want to give it a name so that languages other than C# can access your indexer, you can do it as shown below:

public string this [int indexValue] { }

The indexer shown in this example can be identified using the name TestIndexer.

• Indexers are accessed using the instance pointer “this”. They are directly accessed using the instance name.

• Indexers have two access methods, “get” method is used to fetch values and do manipulations. “set” method is used to set value to an attribute. Any or all of these methods can be used based on the access privilege of the indexer: read-only or write-only or read-write.

• Indexers must include parameters (minimum one parameter) and the parameters are defined inside [ ] brackets.

• Parameters of an Indexer can be of any type, not just integer.

• Indexer cannot be passed as an out parameter or as ref.

• Indexers can never be static.

Usage of Indexer in Classes:

You will understand how to use indexer in classes by browsing the following example:

public class testIndexer {
public string[,] test2DArray = new string[2,2];

public string this[int index1, int index2] {
get {
if(index1>=0 && index1<2 && index2>=0 && index2<2) {
return test2DArray [index1,index2];
else {
return null;
set {
if(index1>=0 && index1<2 && index2>=0 && index2<2) {
test2DArray[index1,index2] = value;
public class useIndexer {
public static void Main( ) {
testIndexer testObj = new testIndexer( );
testObj[0,0] = “sample value1”;
testObj[0,1] = “sample value2”;
testObj[1,0] = “sample value3”;
testObj[1,1] = “sample value4”;
console.WriteLine("{0}\n,{1}\n,{2}\n,{3}\n",{4}\n, testObj[0,0],testObj[0,1],
testObj[1,0], testObj[1,1]);

This example defines an indexer to fetch and store elements into a two dimensional array test2DArray. This indexer defined in testIndexer is then accessed in the class useIndexer. If you look at the way the indexer is used, you will understand that instead of accessing the array element as testObj.test2DArray[0,0] you can directly access it as testObj[0,0] as if the instance in itself is an array. This is achieved using indexers. They provide the same functionality but with less complexity and easy understanding in accessing the element. Not just arrays, you can do any manipulation inside indexers by receiving suitable parameters.

Note that the above code includes range checks for index1 and index2. This range check avoids unwanted occurrence of exceptions. This ensures robustness and reliability of your code.

Usage of Indexer in Interfaces:

Indexer can be used in interfaces. For using it, guidelines are same as the ones indicated in the beginning of this article. In addition, two more constraints are enforced as shown below:

• Indexer in class will have access modifier associated with it. But indexer in interfaces should not include access modifiers. When the interface is implemented by a class, the class can override the indexer with modifiers. But the indexer declared in interface should not include any modifiers.
• Indexers access methods will only be declared and not defined. This is to identify the privileges associated with the indexer. For example, if the indexer has only “get” method defined then the indexer is read-only.
• Indexer is identified using its signature “type this[type variableName]”. This signature should be unique in an interface. i.e., no two indexers can have the same signature in an interface.

Syntax for using indexer in interface is shown below:
public interface <interface name> {
type this[type variableName] {
As discussed, “get” and “set” methods will not have body. Class that implements this interface has to define these methods. Earlier example for using indexer in classes can be modified as shown below to use indexer in interfaces:

public interface testInterface {
string this[int index1, int index2]
class testIndexer: testInterface {
//code is same as the testIndexer class defined in the earlier example
public class useIndexer {
//content is same as useIndexer class defined in earlier example

Here you are defining an interface called testInterface containing indexer declaration. You are then using testIndexer class to implement this interface and this class contains the actual definition of the indexer. Once when the indexer is defined, you are accessing it from useIndexer class. This example will fetch the same results as that of earlier example.


Add to My Yahoo!

FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.


Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage


Recommended Resource

|Designing applications in .Net using Service Oriented Architecture | How to Create and Use Anonymous Delegates in C# (C Sharp) |How to Define Custom Attributes in C# (C Sharp)| How To Handle Errors While Developing ASP.NET Applications | How to Use Indexer in Classes and Interfaces of C# (C Sharp)| Illustration of Access Keywords (base, this) with Examples in C# (C Sharp) | Illustration of Null Coalesce Operator (??) in C# (C Sharp) |Indexers Vs Properties in C# (C Sharp) | More about Reserved Attributes of C# (C Sharp)| Overview of Unified Type System of C# (C Sharp) | Purpose of Delegates in C# (C Sharp) |How does Simple Object Access Protocol (SOAP) help in Internet Communication |How to Create and Use Anonymous Delegates in C# (C Sharp)|


| Privacy Policy for | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.