Share via


C#: When indexers no longer remain cool

Manish was using a library for some image manipulation and hit upon a class which uses indexer. Indexer allows a class/struct to be indexed in the same way as an array. This is typically used by classes that encapsulates some other collection and wants to expose that collection using this technique. Lets first see how a class implements an indexer

 class MyClass{    private int[] arr = new int[100];    publicintthis[int index] // Indexer declaration    {        get {             if (index >= 0 && index < 100)                return arr[index];            else                return 0;        }        set {            if (index >= 0 && index < 100)                arr[index] = value;        }    }};MyClass mc = new MyClass();mc[0] = 10; mc[1] = 20; Console.WriteLine(mc[0] );Console.WriteLine(mc[1]);

Funny thing was that the class implementor simply forgot to expose the length as a property so that in code we can use it for interating through the class as follows

 class MyClass{    private int[] arr = new int[100];    public int this[int index] // Indexer declaration    {        get { ... }        set { ... }
    }
    publicint Length {         get { return arr.Length; }  } };MyClass mc = new MyClass();mc[0] = 10;mc[1] = 20;for(int i = 0; i < mc.Length; i ++)    Console.WriteLine(mc[i]);}

Without the length property being exposed in some means, its totally impossible to index into the class meaningfully.

Moral of the story: If you have a class that uses indexer, expose the length or max value of the index as well....

Comments

  • Anonymous
    August 27, 2006

    Looking your profile, Hyderabad falls under +5:30GMT time zone.

    ~ Ankit

  • Anonymous
    August 28, 2006
    Unless your indexer does not use an actual index, but a string or some such.

    Length (may) not mean anything then.

    Also, the .NET standard is to use .Count and not .Length which is "reserved" for "native .NET" objects such as Arrays and Strings.

    Just a thought.

  • Anonymous
    August 28, 2006
    Also..

    I know the code is just for illustration purposes, but I find you returning zero for out of bounds to be very disturbing.

    (Of course, may be completely valid for the scenario for which this code is applicable, but still.....)

  • Anonymous
    August 28, 2006
    Yep returning 0 is very disturbing and should throw an Out of bound exception. However the MSDN site had this sample and I should've been more careful about it http://msdn2.microsoft.com/en-us/library/2549tw02.aspx

    In case the Indexer is used as an lookup table using string indexes then there should be no length and count as you cannot possible iterate through it anyway.

  • Anonymous
    September 29, 2006
    The comment has been removed

  • Anonymous
    September 22, 2008
    What about setting the length dynamically? Can we have logic to set the length from such class member?

  • Anonymous
    September 22, 2008
    Length should be a property. So even if the length is dynamic it should work because it is going to be queried each time