Monday, July 26, 2004

 

Indexers In C#



Indexers In C#


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.

 <modifier> <return type> 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

// Author: miteshvmehta@gmail.com

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]);
}
}


The indexers in C# can be overloaded just like
member functions. The formal parameter list of an indexer defines the
signature of the indexer. Specifically, the signature of an indexer
consists of the number and types of its formal parameters. The element
type is not part of an indexer's signature, nor is the names of the
formal parameters. The signature of an indexer must differ from the
signatures of all other indexers declared in the same class. C# do not
have the concept of static indexers. If we declare an indexer static,
the compiler will show a compilation time error.

Indexers & Inheritance


Just like any other class members, indexers
can also participate in inheritance. A base class indexer is inherited
to the derived class.

//C#: Indexer : Inheritance

//Author: miteshvmehta@gmail.com
using System;
class Base
{
public int this[int indxer]
{
get
{
Console.Write("Base GET");
return 10;
}
set
{
Console.Write("Base SET");
}
}
}

class Derived : Base
{

}
class MyClient
{
public static void Main()
{
Derived d1 = new Derived();
d1[0] = 10;
Console.WriteLine(d1[0]);//Displays 'Base SET Base GET 10'
}
}


Indexers & Polymorphism


A Base class indexer can be polymorphicaly
overridden in a Derived class. But remember that the modifiers like
virtual, override etc are using at property level, not at accessor
level.

//C#: Indexer : Polymorphism

//Author: miteshvmehta@gmail.com

using System;

class Base
{
public virtual int this[int index]
{
get
{
Console.Write("Base GET");
return 10;
}
set
{
Console.Write("Base SET");
}
}
}

class Derived : Base
{
public override int this[int index]
{
get
{
Console.Write("Derived GET");
return 10;
}
set
{
Console.Write("Derived SET");
}
}
}

class MyClient
{
public static void Main()
{
Base b1 = new Derived();
b1[0]= 10;
Console.WriteLine(b1[0]);//Displays 'Derived SET Derived GET 10'
}
}


Abstract Indexers


An indexer inside a class can be declared as
abstract by using the keyword abstract. Remember that an abstract
indexer in a class carries no code at all. The get/set accessors are
simply represented with a semicolon. In the derived class we must
implement both set and get assessors.


If the abstract class contains only set accessor, we can implement only set in the derived class.


The following program shows an abstract indexer in action.

//C#: Indexer : Abstract

//Author: miteshvmehta@gmail.com

using System;

abstract class Abstract
{
public abstract int this[int index]
{
get;
set;
}
}

class Concrete : Abstract
{
public override int this[int index]
{
get
{
Console.Write(" GET");
return 10;
}
set
{
Console.Write(" SET");
}
}
}

class MyClient
{
public static void Main()
{
Concrete c1 = new Concrete();
c1[0] = 10;
Console.WriteLine(c1[0]);//Displays 'SET GET 10'
}
}




Indexers & Properties


1. An index is identified by it's signature. But a property is identified it's name.

2. An indexer is always an instance member, but a property can be static also.

3. An indexer is accessed through an element access. But a property is through a member access.


Conclusion


The indexer is one of the key concepts of C#
and are very interesting aspects of the language.



Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?