I\'ve seen lot of examples for c# Indexers, but in what way will it help me in real life situations.
I know the C# guru wouldn\'t have added this if it wasn\'t a ser
The way I look at indexers is that (rightly or wrongly!), accessing something by index should be more efficient than accessing it any other way, because in some way, shape or form, the class whose indexer I'm using stores some form of index that allows it to quickly lookup values when accessed that way.
The classic example is an array, when you access element n of an array by using the code myarray[3], the compiler/interpreter knows how big (memory-wise) elements of the array are and can treat it as an offset from the start of the array. You could also "for(int i = 0; i < myarray.length; i++) { if (i = 3) then { .. do stuff } }"
(not that you'd ever want to!), which would be less efficient. It also shows how an array is a bad example.
Say you had a collection class that stores, umm, DVDs, so:
public class DVDCollection
{
private Dictionary<string, DVD> store = null;
private Dictionary<ProductId, string> dvdsByProductId = null;
public DVDCollection()
{
// gets DVD data from somewhere and stores it *by* TITLE in "store"
// stores a lookup set of DVD ProductId's and names in "dvdsByProductid"
store = new Dictionary<string, DVD>();
dvdsByProductId = new Dictionary<ProductId, string>();
}
// Get the DVD concerned, using an index, by product Id
public DVD this[ProductId index]
{
var title = dvdsByProductId[index];
return store[title];
}
}
Just my 2p, but, like I said,.. I've always considered an "indexer" as being an expedient way of getting data out of something.
In ASP.Net, there are a few different times where an indexer is used such as reading something from any of the Request, Session or Application objects. I've seen often where something is stored in either the Session or Application object only to be used again and again.
Microsoft has an example using an indexer to treat a file as an array of bytes.
public byte this[long index]
{
// Read one byte at offset index and return it.
get
{
byte[] buffer = new byte[1];
stream.Seek(index, SeekOrigin.Begin);
stream.Read(buffer, 0, 1);
return buffer[0];
}
// Write one byte at offset index and return it.
set
{
byte[] buffer = new byte[1] {value};
stream.Seek(index, SeekOrigin.Begin);
stream.Write(buffer, 0, 1);
}
}
It is just syntactical sugar for collection type classes. I never had a reason to write such a class. So I think there are very rare uses of it in "real life", because the classes using it are already implemented.
http://code-kings.blogspot.in/2012/09/indexers-in-c-5.html
using System;
namespace Indexers_Example
{
class Indexers
{
private Int16[] RollNumberVariable;
public Indexers(Int16 size)
{
RollNumberVariable = new Int16[size];
for (int i = 0; i < size; i++)
{
RollNumberVariable[i] = 0;
}
}
public Int16 this[int pos]
{
get
{
return RollNumberVariable[pos];
}
set
{
RollNumberVariable[pos] = value;
}
}
}
}
Once .NET got generics, the biggest reason for me to implement an indexer (to implement a strongly-typed collection) went away.