Why use indexers?

This is a discussion on Why use indexers? within the C# Programming forums, part of the General Programming Boards category; Hi! Can someone enlighten me as to when, where, and why one would use a class indexer? I am trying ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    12

    Question Why use indexers?

    Hi!

    Can someone enlighten me as to when, where, and why one would use a class indexer? I am trying to come up with an example which features the benefits of indexers--and am coming up dry. Is there more to it than saving a method declaration? So, please share with me what they are actually used for and what the benefit is.

    Jess

    C# Online.NET

    http://wiki.csharp-online.net/

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by MSDN
    Defining an indexer allows you to create classes that act like "virtual arrays." Instances of that class can be accessed using the [] array access operator. Defining an indexer in C# is similar to defining operator [] in C++, but is considerably more flexible. For classes that encapsulate array- or collection-like functionality, using an indexer allows the users of that class to use the array syntax to access the class.
    http://msdn.microsoft.com/library/de...rstutorial.asp

    It basically lets you define a member access on classes that are similar in syntax to member access on arrays. Like the article says, it's most often used on collection classes.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    12
    Yes, what I am trying to say is that in the examples I've seen, I can see no advantage to using an indexer vs. using regular method calls. So what am I missing?

    In the tutorial (http://msdn.microsoft.com/library/de...stutorial.asp), for example, one can *already* access the file as a byte array, so why write a class to do so?

    Just an example--sure; but, that's what I am saying: Where are the good examples that have real benefits?

    Just an alternative--okay; but, why would I choose this alternative?

    If indexers are for "array-like" abstractions, why not use an array?

    Jess

    C# Online.NET

    http://wiki.csharp-online.net/

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Code:
    namespace Enviroment1
    {
        public class Test
        {
            public static void Main( )
            {
                int numberOfArrays = 5;
                MyCollecionClass mcc = new MyCollecionClass(numberOfArrays);
                mcc[0] = "one";
                mcc[1] = "two";
                mcc[2] = "three";
                mcc[3] = "four";
                mcc[4] = "five";
    
                for ( int i = 0; i < numberOfArrays; ++i )
                {
                    Console.WriteLine(mcc[i]);
                }
            }
        }
    
        public class MyCollecionClass
        {
            private string[] array;
    
            public MyCollecionClass( int arrayLength )
            {
                array = new string[arrayLength];
            }
    
            public string this[int index]
            {
                get
                {
                    if ( (index >= 0) && (index < array.Length) )
                    {
                        return array[index];
                    }
    
                    Console.WriteLine("out of bounds!");
                    return null;
                }
                set
                {
                    if ( (index >= 0) && (index < array.Length) )
                    {
                        array[index] = value;
                    }
                }
            }
        }
    }
    I don't know about you, but, to me this indexer is much more easier and intuitive than, say, create a method as you suggested. How would you do it with a method?

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    12
    The code below does the same thing without an indexer.

    I am not really arguing against indexers--I just don't get the point. I wish someone sympathetic to indexers would write an article on them for us. I am afraid I won't do them justice.

    Jess

    C# Online.NET

    http://wiki.csharp-online.net/


    Code:
    public class Test
        {
            public static void Main( )
            {
                int numberOfArrays = 5;
                MyCollectionClass mcc = new MyCollectionClass(numberOfArrays);
                mcc.set(0, "one");
                mcc.set(1, "two");
                mcc.set(2, "three");
                mcc.set(3, "four");
                mcc.set(4, "five");
    
                for ( int i = 0; i < numberOfArrays; ++i )
                {
                    Console.WriteLine(mcc.get(i));
                }
             }
        }
    
        public class MyCollectionClass
        {
    	 private string[] array;
    
    	 public string get (int index) { return array[index]; }
    	 public void   set (int index, string value) { array[index] = value; }
    
            public MyCollectionClass ( int arrayLength )
            {
                array = new string[arrayLength];
            }
    
        }

  6. #6
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    You can't always use the built-in arrays to do what you need to do. For instance, it can be useful to have collections that notify you via events when items are added, removed, or changed. It can be useful to use strings, floats, or user-defined classes as the key to retrieve members from your collection. Allowing users to create indexers lets users keep a familiar syntax for member access, increasing the readability of the code.

    As for benefits, I can't say for certain, but I can suggest. The same argument you're making against indexers could apply against all properties in general. Why use properties when you could just create methods to do the same thing? Since properties are often inlined by the compiler for efficiency, an indexer might be a way of allowing safe and optimized access to collection members. Using methods for this would likely be slower due to the overhead of a function call.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    12
    Actually, I would make the same argument about properties as opposed to simply using accessors and mutators to encapsulate. To me, they both seem like guilding the lily.

    Jess

    C# Online.NET

    http://wiki.csharp-online.net/

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21