Click here to Skip to main content
15,499,962 members
Articles / Programming Languages / C#
Posted 17 Jan 2006


17 bookmarked

Multiple Indexers In a Class Using Interface Indexers

Rate me:
Please Sign up or sign in to vote.
3.07/5 (11 votes)
17 Jan 20061 min read
Using multiple indexers in a class using interface indexers.


We all know about class indexers. Well, you can also have indexers in interfaces.

Interface indexers differ from class indexers in the following ways:

  • Interface accessors do not use modifiers.
  • An interface accessor does not have a body.
  • The purpose of the accessor is to indicate whether the indexer is read-write, read-only, or write-only.

The following is an example of an interface indexer:

  1. Open a new console project.
  2. Create two interfaces, i.e., Interface1 and Interface2 (as shown below in the code).
  3. Implement the indexer in each of the interfaces as shown below.
  4. Create a class called as MyClass (as shown below) which implements these two interfaces.


class MyClass:Interface1,Interface2
  private int[] ArrayofStrings=new int[100];
  public MyClass()
  public int this[int index] /* Interface1 */
    return ArrayofStrings[index];
  string Interface2.this[int index]
    return ArrayofStrings[index].ToString() + " String";
public class MainClass
  public static void Main()
   MyClass o = new MyClass();

   o[1] = 1; // interface1

   // Interface2

public interface Interface1
  int this[int index]

public interface Interface2
  string this[int index]

You will notice that this class has two indexers, one coming from the Interface1 and other coming from Interface2. Indexer function for Interface1 has the access modifier as public whilst the indexer for Interface2 is private. Since the access modifier for indexer of Inferface1 is public, you can access it directly from the main as you would have accessed any other class indexer.

But you will notice that along with indexer for Interface1, you can also access the indexer for Interface2 by type casting, because the Interface2 is public.

Hence, in this way, you can have multiple indexers in a class using interface indexers.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Written By
United States United States
Namratha Shah a.k.a. Nasha is from orginally from Bombay, India but currently residing NJ, USA. She has to her credit, a Bachelor’s Degree in Microbiology and Biotechnology and a Master's in Computer and Software Applications (1999-2001) from Somaiya College Bombay. She started her career with C and C++ and then moved on to Microsoft Technologies. She has over 7.5 years experience in software architecture, design and development. She is a Certified Scrum Master and a member of the CORE .NET Architecture team. She has been Awarded with Microsoft’s Prestigious Most Valuable Professional (MVP) twice consecutively in years 2005 and 2006 in Visual C#.NET for her outstanding contributions to the .NET community.

Comments and Discussions

GeneralTimely article... Pin
Marc Clifton17-Jan-06 8:20
mvaMarc Clifton17-Jan-06 8:20 
GeneralRe: Timely article... Pin
binaire19-Jan-06 2:19
Memberbinaire19-Jan-06 2:19 
QuestionWhere could this help? Pin
Pop Catalin17-Jan-06 8:11
MemberPop Catalin17-Jan-06 8:11 
AnswerRe: Where could this help? Pin
Peter Hancock21-May-07 19:15
MemberPeter Hancock21-May-07 19:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.