Click here to Skip to main content
6,595,444 members and growing! (21,657 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

Understanding Indexer in C#

By Arul Nayagam C

This article explains about the indexer in C# programming with simple program.
C#, Windows, .NET, Visual Studio, Dev
Posted:25 Sep 2005
Views:34,652
Bookmarked:7 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
14 votes for this article.
Popularity: 2.00 Rating: 1.74 out of 5
8 votes, 57.1%
1
2 votes, 14.3%
2
2 votes, 14.3%
3
1 vote, 7.1%
4
1 vote, 7.1%
5

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;


namespace IndexerExample
{

Class MyPreviousExp
{
private string[] myCompanies = new string[10];

//index creation
public string this[int index]
{

get
{
if(index <0 or index >= 6)
return "null";
else
return myCompanies[index];

}
set
{
if(!(index <0 or index >= 10))
myCompanies[index] = value;
}

}
}
Class myMainClass
{
public static void Main()
{
myPreviousExp indexerObj = new myPreviousExp();

indexerObj[0] = "AMS"
indexerObj[3] = "HCL"
indexerObj[5] = "ACC"
for(int i=0; i<10; i++
{

Console.WriteLine(" My Companies{0} : {1} ",i,indexerObj[i]);
}

}
}

}

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


Member
Working as a Software Engineer in a MNC Company.Knowledge on C#.NET, ASP.NET, VB.NET ,VB & SQL Server.
Occupation: Web Developer
Location: India India

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
GeneralMy vote of 1 PinmemberYSDen12318:35 6 Jun '09  
GeneralC# indexer PinmemberJames Quinn20:04 7 Mar '08  
GeneralIndexer Pinmemberanilkumar_a21:33 22 Mar '07  
GeneralRe: Indexer Pinmembernarsinh0:39 22 Nov '07  
GeneralIndexer PinmemberSreejith SS Nair23:43 25 Sep '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 25 Sep 2005
Editor:
Copyright 2005 by Arul Nayagam C
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project