Click here to Skip to main content
Licence 
First Posted 25 Sep 2005
Views 57,860
Bookmarked 11 times

Understanding Indexer in C#

By | 25 Sep 2005 | Article
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

About the Author

Arul Nayagam C

Web Developer

India India

Member

Working as a Software Engineer in a MNC Company.Knowledge on C#.NET, ASP.NET, VB.NET ,VB & SQL Server.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberKamba_prj22:49 25 Oct '11  
QuestionWhat for this article? Nothing new, nothing interesting! and also so may error in your code Pinmemberarjunmca17:48 14 Aug '11  
QuestionWhat for this article? Nothing new, nothing interesting! and also so may error in your code Pinmemberarjunmca17:48 14 Aug '11  
GeneralMy vote of 1 PinmemberYSDen12317:35 6 Jun '09  
GeneralC# indexer PinmemberJames Quinn19:04 7 Mar '08  
GeneralIndexer Pinmemberanilkumar_a20:33 22 Mar '07  
GeneralRe: Indexer Pinmembernarsinh23:39 21 Nov '07  
GeneralIndexer PinmemberSreejith SS Nair22:43 25 Sep '05  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 26 Sep 2005
Article Copyright 2005 by Arul Nayagam C
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid