Click here to Skip to main content
15,885,757 members
Articles / Programming Languages / C#
Article

Understanding Indexer in C#

Rate me:
Please Sign up or sign in to vote.
1.59/5 (21 votes)
25 Sep 20052 min read 108.4K   11   10
This article explains about the indexer in C# programming with simple program.

Introduction

Indexer:

If we heard the word indexers or index, the name "Array" will come to our mind.
Arrays are accessed by using the index.In the same way in C# also, Indexer is used to access the class instances by means of index notation.


Syntax for Declaration:

public type this [type identifier]

Ex : public string this [int myIndex]


Declaring Indexer

public string this[int myCompanies]
{
 get{

  return  mycompanies[index]
    }

 set{

    }
}

Simple Example using Indexer in C#

using System;<BR><BR><BR>namespace IndexerExample<BR>{<BR><BR>Class MyPreviousExp<BR>{ <BR> private string[] myCompanies = new string[10]; <BR><BR> //index creation<BR> public string this[int index]<BR> {<BR> <BR>  get<BR>  {<BR>   if(index <0 or index >= 6)<BR>    return "null"; <BR>   else<BR>    return myCompanies[index];<BR>    <BR>  } <BR>  set<BR>  {<BR>   if(!(index <0 or index >= 10))<BR>    myCompanies[index] = value;<BR>  }<BR> <BR> } <BR>} <BR>Class myMainClass<BR>{<BR> public static void Main()<BR> {<BR>  myPreviousExp indexerObj = new myPreviousExp();<BR>  <BR>  indexerObj[0] = "AMS"<BR>  indexerObj[3] = "HCL"<BR>  indexerObj[5] = "ACC" <BR>  for(int i=0; i<10; i++<BR>  {    <BR>   <BR>   Console.WriteLine(" My Companies{0} : {1} ",i,indexerObj[i]);<BR>  }<BR>   <BR> }<BR>} <BR><BR>}<BR>

Description:

Here i have created a class named myPreviousExp and having a memeber called myCompanies which is
a array of string.The index is created using the this operator and set and get accessor are same
as we use in properties.

Better to know the difference between properties and indexer to have clear idea

property can have unique name whereas the indexer can have uniuqe signature.

Prperty is accessed through its name(like obj.mycolor: here mycolor is property)
whereas Indexer is accessed through an element (obj[1] = "abc" : here we are assigning abc to the first element of indexer)

Then in Main Class, The Indexer object is created using the indexer myPreviousExp which we defined earlier using the following statement
 
myPreviousExp indexerObj = new myPreviousExp();

Now the indexer is created which is having 20 Elements in it. The indexer is accessed through its elemets like

  indexerObj[0] = "AMS"
  indexerObj[3] = "HCL"
  indexerObj[5] = "ACC"

Here only first , third and fifth elements are accessed and assigned with string values.because we have created the
indexer of type string.

In next step, if we print the indexer using the for loop we can see the result as follows.

myCompanies 0 : AMS
myCompanies 1 :
myCompanies 2 :
myCompanies 3 : HCL
myCompanies 4 :
myCompanies 5 : ACC
myCompanies 6 : null
myCompanies 7 : null
myCompanies 8 : null
myCompanies 9 : null

If we notice the above result, element 0,element 3,element 5 are assigned with values and element 6 to 9 are assigned with
null vaules.This is done in the indexer get accessor statement.


Summary

About Indexer
.Gives the Array like access to class instances and members
.Indexer can be overloaded

Hope this tutorilal will help you understand about indexer in C#
any queries reach me in arul_tony@yahoo.com

License

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
Web Developer
India India
Working as a Software Engineer in a MNC Company.Knowledge on C#.NET, ASP.NET, VB.NET ,VB & SQL Server.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Member 1055236123-Feb-14 18:56
Member 1055236123-Feb-14 18:56 
GeneralMy vote of 1 Pin
Member 1055236123-Feb-14 18:21
Member 1055236123-Feb-14 18:21 
GeneralMy vote of 5 Pin
Kamba_prj25-Oct-11 22:49
Kamba_prj25-Oct-11 22:49 
QuestionWhat for this article? Nothing new, nothing interesting! and also so may error in your code Pin
arjunmca14-Aug-11 17:48
arjunmca14-Aug-11 17:48 
QuestionWhat for this article? Nothing new, nothing interesting! and also so may error in your code Pin
arjunmca14-Aug-11 17:48
arjunmca14-Aug-11 17:48 
GeneralMy vote of 1 Pin
YSDen1236-Jun-09 17:35
YSDen1236-Jun-09 17:35 
What for this article? Nothing new, nothing interesting! Just to write article on CodeProject?
GeneralC# indexer Pin
James Quinn7-Mar-08 19:04
James Quinn7-Mar-08 19:04 
GeneralIndexer Pin
anilkumar_a22-Mar-07 20:33
anilkumar_a22-Mar-07 20:33 
GeneralRe: Indexer Pin
narsinh21-Nov-07 23:39
narsinh21-Nov-07 23:39 
GeneralIndexer Pin
sreejith ss nair25-Sep-05 22:43
sreejith ss nair25-Sep-05 22:43 

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.