0

I'm learning about Indexers and stumble across the explanation and example of the tutorial I'm reading.

It says:

"An indexer allows an object to be indexed such as an array. When you define an indexer for a class, this class behaves similar to a virtual array. You can then access the instance of this class using the array access operator ([ ])"

What I understand with this paragraph is that you can access an instance of that class using the array access operatos.

But what I really dont understand of this explanation is the following:

"Declaration of behavior of an indexer is to some extent similar to a property. similar to the properties, you use get and set accessors for defining an indexer. However, properties return or set a specific data member, whereas indexers returns or sets a particular value from the object instance. In other words, it breaks the instance data into smaller parts and indexes each part, gets or sets each part".

I dont get the "It breaks the instance data into smaller parts and indexes each part"

After this it gives an example of Indexer:

using System;
namespace IndexerApplication
{
   class IndexedNames
   {
      private string[] namelist = new string[size];
      static public int size = 10;
      public IndexedNames()
      {
         for (int i = 0; i < size; i++)
         namelist[i] = "N. A.";
      }

      public string this[int index]
      {
         get
         {
            string tmp;

            if( index >= 0 && index <= size-1 )
            {
               tmp = namelist[index];
            }
            else
            {
               tmp = "";
            }

            return ( tmp );
         }
         set
         {
            if( index >= 0 && index <= size-1 )
            {
               namelist[index] = value;
            }
         }
      }

      static void Main(string[] args)
      {
         IndexedNames names = new IndexedNames();
         names[0] = "Zara";
         names[1] = "Riz";
         names[2] = "Nuha";
         names[3] = "Asif";
         names[4] = "Davinder";
         names[5] = "Sunil";
         names[6] = "Rubic";
         for ( int i = 0; i < IndexedNames.size; i++ )
         {
            Console.WriteLine(names[i]);
         }

         Console.ReadKey();
      }
   }
}

Before that paragraph I thought Indexers where a form to index and instance of that class as an array, but that "smaller parts" I really dont understand.

Nickso
  • 785
  • 1
  • 10
  • 32
  • 1
    All it's saying is that it's a way to access part of your class - you can index into a string to get a single `char` out of the string, e.g. `char c = string[0]`. You can index into a list to get a single item (e.g. a smaller part) out of the list `var item = list[1];`. – Dave Zych Sep 25 '15 at 16:22
  • It is woolly language, they are trying to avoid being too specific. So lets get specific, you should write an indexer when your class behaves like a collection. – Hans Passant Sep 25 '15 at 16:32
  • Hi Hans and thanks for a real use of Indexers. When you say "When your class behaves like a collection" you mean a class member of that class being a collection? Would you give me an example of a real life class that behaves like a collection? – Nickso Sep 25 '15 at 16:48

1 Answers1

0

It's really just giving an example of the use of an indexer. Returning an element from the array is, in a way, returning part of the array.

But it needn't be an array. For example, String has an indexer which returns the character at that position, e.g. "test"[0] returns the character in position zero, which is 't'.

Other objects may have other indexers which do other things. For example a DataTable DataRowCollection has an indexer which returns rows by number, and which has an indexer which returns columns by name:

myDataTable.Rows[12]["ID"].Value
Ben
  • 34,935
  • 6
  • 74
  • 113
  • So that is what it means regarding "A smaller part" of the object. if all the elements in an array is the entire array, returning an element of that array is that "Small Part". Same goes with the string (The example you gave). So in this example is creating an array of strings, it completes it with "N. A" . This array is a member class of the class IndexedNames, if you want the 1st element of the array, why dont you call for example 'namelist[0]'? – Nickso Sep 25 '15 at 16:40