Microsoft.NET

……………………………………………….Expertise in .NET Technologies

Indexer in C# (OOP’s) – Part 9

Posted by Ravi Varma Thumati on July 13, 2009

Indexer

Indexers are also called smart arrays in C# and can be used to treat an object as an array. An indexer allows an instance of a class or a struct to be indexed as an array, which is useful for looping or iterating or data binding operations.

The following is the syntax of an indexer declaration.

<Modifier> <Return type> this[arguments]

{

get

{

}

Set

{

}

}

The modifier can be one of the following

  • private
  • public
  • protected
  • internal

All indexers should accept at least one parameter. Indexers cannot be static. This is because static methods do not have access to ‘this’. The ‘this’ keyword indicates an instance of the current class. Look at the code given below.

A simple indexer

namespace ConsoleApplication

{

using System;

class Employee

{

private string[]name = new string[10];

public string this[int index]

{

get

{

return name[index];

}

set

{

name[index] = value;

}

}

}

class Test

{

public static void Main()

{

Employee emp = new Employee();

emp[0] = “Joydip”;

emp[1] = “Manashi”;

emp[2] = “Jini”;

Console.WriteLine(“The namesare:–“);

for (int i = 0; i < 3;Console.WriteLine(emp[i++]))

;

Console.ReadLine();

}

}

}

The output of the program is

The names are:–

Joydip

Manashi

Jini

Indexers in inheritance

The indexers of the base class are inherited to its derived classes. This is illustrated in the code shown below.

using System;

class Base

{

public int number;

public int this[int index]

{

get

{

Console.Write(“Get of the baseclass.”);

return number;

}

set

{

Console.Write(“Set of the baseclass.”);

number = value;

}

}

}

class Derived: Base{}

class Test

{

public static void Main()

{

Derived d = new Derived();

d[0] = 500;

Console.WriteLine(“The value is: “+ d[0]);

}

}

The program displays the string “Set of the base class. Get of the base class. The value is: 500”.

Indexers can be polymorphic. We can have the same indexer in the base and the derived classes. Such indexers are ‘overridden indexers’ and require the keyword virtual to be stated in their declaration in the base class. The following program illustrates this.

Indexers can be polymorphic

using System;

class Base

{

protected int number;

public virtual int this[int index]

{

get

{

Console.Write(“Get of the baseclass.”);

return number;

}

set

{

Console.Write(“Set of the baseclass.”);

number = value;

}

}

}

class Derived: Base

{

public override int this[int index]

{

get

{

Console.Write(“Get of the derivedclass.”);

return base.number;

}

set

{

Console.Write(“Set of the derivedclass.”);

base.number = value;

}

}

}

class Test

{

public static void Main()

{

Base obj = new Derived();

obj[0] = 500;

Console.WriteLine(“The value is: “+ obj[0]);

}

}

The output of the program is “Set of the derived class. Get of the derived class. The value is: 500”.

Abstract indexers

Indexers can be abstract. We can have abstract indexers in a base class that need to be implemented by the class subclassing (inheriting from) it. The abstract indexer like an abstract method does not contain any code in its get or set. This is shown in the example below.

Indexers can be abstract

using System;

abstract class Base

{

protected int number;

public abstract int this[int index]

{

get;

set;

}

}

class Derived: Base

{

public override int this[int index]

{

get

{

return number;

}

set

{

number = value;

}

}

}

class Test

{

public static void Main()

{

Derived obj = new Derived();

obj[0] = 500;

Console.WriteLine(“The value is: “+ obj[0]);

}

}

The output of the program is “The value is: 500”.

Comparison between properties and indexers

Properties are accessed by names but indexers are accessed using indexes. Properties can be static but we cannot have static indexers. Indexers should always be instance members of the class. The get accessor of a property does not accept any parameter but the same of the indexer accepts the same formal parameters as the indexer. The set accessor of a property contains an implicit parameter called “value”. The set accessor of an indexer contains the same formal parameters as that of the indexer and also the “value” as an implicit parameter.

Conclusion

Properties and indexers are two of the most important concepts in C#. This article has discussed both these concepts with code examples. I would highly appreciate any comments and or suggestions from the readers related to this article. Please post your comments and or suggestions if you have any. Happy reading!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: