Click here to Skip to main content
11,573,432 members (61,099 online)
Click here to Skip to main content

Indexers In C#

, 6 Sep 2006 CPOL 28.8K 81 11
Rate this:
Please Sign up or sign in to vote.
C# introduces a new concept known as Indexers which are used for treating an object as an array. The indexers are usually known as smart arrays in C# community.

Introduction

C# introduces a new concept known as Indexers which are used for treating an object as an array. The indexers are usually known as smart arrays in C# community. Defining a C# indexer is much like defining properties. We can say that an indexer is a member that enables an object to be indexed in the same way as an array.

this [argument list] 
{ 
    get 
    { 
        // Get codes goes here 
    } 
    set 
    { 
        // Set codes goes here 
    } 
} 

Where the modifier can be private, public, protected or internal. The return type can be any valid C# types. The 'this' is a special keyword in C# to indicate the object of the current class. The formal-argument-list specifies the parameters of the indexer. The formal parameter list of an indexer corresponds to that of a method, except that at least one parameter must be specified, and that the ref and out parameter modifiers are not permitted. Remember that indexers in C# must have at least one parameter. Other wise the compiler will generate a compilation error.


The following program shows a C# indexer in action

// C#: INDEXER 
using System; 
using System.Collections; 

class MyClass 
{ 
    private string []data = new string[5]; 
    public string this [int index] 
    { 
       get 
       { 
           return data[index]; 
       } 
       set 
       { 
           data[index] = value; 
       } 
    } 
}

class MyClient 
{ 
   public static void Main() 
   { 
      MyClass mc = new MyClass(); 
      mc[0] = "Rajesh"; 
      mc[1] = "A3-126"; 
      mc[2] = "Snehadara"; 
      mc[3] = "Irla"; 
      mc[4] = "Mumbai"; 
      Console.WriteLine("{0},{1},{2},{3},{4}",mc[0],mc[1],mc[2],mc[3],mc[4]); 
   } 
} 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

kalyan.Bandarupalli
techbubbles.com
India India
WWW.TECHBUBBLES.COM

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 1 Pin
David Catriel26-Apr-11 16:23
memberDavid Catriel26-Apr-11 16:23 
GeneralMy vote of 2 Pin
PavelGnelitsa21-Jun-09 21:23
memberPavelGnelitsa21-Jun-09 21:23 
GeneralComments Pin
Andrew Phillips10-Oct-07 18:42
memberAndrew Phillips10-Oct-07 18:42 
GeneralSimple and Good Example Pin
ViReNdrA KuMaR KoHli19-Jul-07 1:02
memberViReNdrA KuMaR KoHli19-Jul-07 1:02 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150624.2 | Last Updated 6 Sep 2006
Article Copyright 2006 by kalyan.Bandarupalli
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid