Tuesday, March 24, 2009

Indexers

Indexers treats object as an array or collection, we can access the indexer’s elements using square brackets ([]) it enables an object to be indexed and provide array-like or collection-like access to the classes. Indexer defined much like properties. C# developers also called this as smart arrays.

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

Eg.
namespace ConsoleApplication1
{
public class Customer
{
public int Id;
public string Name;
public string City;
public Customer(int id, string name, string city)
{
Id = id;
Name = name;
City = city;
}
}

class SampleCollection
{
// Declare an array to store the data elements.
private T[] arr = new T[100];

// Define the indexer, which will allow client code
// to use [] notation on the class instance itself.
// (See line 2 of code in Main below.)
public T this[int i]
{
get
{
// This indexer is very simple, and just returns or sets
// the corresponding element from the internal array.
return arr[i];
}
set
{
arr[i] = value;
}
}
}

class MyPoint
{
static void Main()
{
SampleCollection stringCollection = new SampleCollection();

// Use [] notation on the type.
stringCollection[0] = "Hello, World";
stringCollection[1] = "Mohammed Ghouse";
Console.WriteLine(stringCollection[0]);
Console.WriteLine(stringCollection[1]);

SampleCollection stringCollection2 = new SampleCollection();
stringCollection2[0] = 786;
stringCollection2[1] = 123;
Console.WriteLine(stringCollection2[0]);
Console.WriteLine(stringCollection2[1]);

SampleCollection stringCollection3 = new SampleCollection();

Customer cust1 = new Customer(1,"Ghouse","Kadapa");
Customer cust2 = new Customer(2, "Mohammed", "Bangalore");

stringCollection3[0] = cust1;
stringCollection3[1] = cust2;
Console.WriteLine(stringCollection3[0].Name);
Console.WriteLine(stringCollection3[1].Name);

Console.ReadLine();
}

}
}


1. this keyword refers to the object to which indexer belongs
2. ref and out parameter modifiers are not permitted
3. Indexers can be overloaded just like member functions
4. Indexer doesn’t have a name; it is recognized by its signature - types and number of parameters
5. There is nothing called static indexers
6. A base class indexer is inherited and can be overridden in the derived class.


class BaseClass
{
public virtual int this[int index]
{
get
{
Console.Write("from BaseClass Get assessors");
return 1;
}
set
{
Console.Write("from BaseClass Set assessors");
}
}
}
class DerivedClass : BaseClass
{
public override int this[int index]
{
get
{
Console.Write("from DerivedClass Get assessors");
return 2;
}
set
{
Console.Write("from DerivedClass Set assessors");
}
}
}
class MyClient
{
public static void Main()
{
BaseClass obj1 = new DerivedClass();
obj1[0]= 123;
Console.WriteLine(obj1[0]);
}
}
7. To avoid index errors it’s always better to check index limits in get and set assessors.
Interfaces can have indexers with the exception that it's indexers don’t have modifiers and obviously no implementation of assessors in interfaces, below is the e.g.

public interface ISomeInterface
{
// Indexer declaration:
string this[int index]
{
get;
set;
}

}

No comments:

Post a Comment