C-Indexer

C-Indexer

Indexer allows an object to be indexed like an array. When you define an indexer for a class, it behaves like a virtual array. You can use the array access operator ([]) to access instances of this class.

grammar

The syntax of the one-dimensional indexer is as follows:

element-type this[int index] 
{
   // get accessor
   get 
   {
      // Returns the value specified by index
   }

   // set accessor
   set 
   {
      // Set the value specified by index 
   }
}

Purpose of Indexer

The declaration of the indexer's behavior is somewhat similar to that of a property. Just like properties, you can use get and set accessors to define indexers. However, properties return or set a specific data member, while indexers return or set a specific value for an object instance. In other words, it divides the instance data into smaller parts, indexes each part, and gets or sets each part.

Defining a property includes providing the property name. Indexer is defined without a name, but with this keyword, which points to an object instance. The following example demonstrates this concept:

Example

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();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

Zara
Riz
Nuha
Asif
Davinder
Sunil
Rubic
N. A.
N. A.
N. A.

Overload Indexer

Indexers can be overloaded. Indexers can also be declared with multiple parameters, each of which can be of a different type. There is no need for indexers to be integer. C allows indexers to be of other types, such as string types.

The following example demonstrates an overloaded indexer:
Example

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;
            }
         }
      }
      public int this[string name]
      {
         get
         {
            int index = 0;
            while(index < size)
            {
               if (namelist[index] == name)
               {
                return index;
               }
               index++;
            }
            return index;
         }

      }

      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";
         // Use the first indexer with the int parameter
         for (int i = 0; i < IndexedNames.size; i++)
         {
            Console.WriteLine(names[i]);
         }
         // Using a second indexer with a string parameter
         Console.WriteLine(names["Nuha"]);
         Console.ReadKey();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

Zara
Riz
Nuha
Asif
Davinder
Sunil
Rubic
N. A.
N. A.
N. A.
2

You can also customize the indexer size when class is instantiated
Example:

public class IndexedNames
{
  private string[] nameList;
  public int size;
  public IndexedNames(int size)
  {
    this.size = size;
    nameList = new string[size];
    for(int i = 0;i < size; i++)
    {
      nameList[i]="N. A.";
    }
  }
  public string this[int index]
  {
    get
    {
      string temp;
      if(index>=0 && index<size)
      {
        temp = nameList[index];
      }
      else
      {
        temp = "";
      }
      return temp;
    }
    set
    {
      if(index>=0 && index<size)
      {
        nameList[index] = value;
      }
    }
  }
}
static void Main(string[] args)
{
   IndexedNames index = new IndexedNames(5);
   index[0] = "Zara";
   index[1] = "Riz";
   index[2] = "Nuha";
   index[3] = "Asif";
   index[4] = "Davinder";
   for (int i = 0; i < index.size; i++)
   {
      Console.WriteLine(index[i]);
   }
   Console.ReadKey();
}
using System;

namespace IndexerApplication{
    //1. When the class member variable is an array
    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]);
             }
         }
     }
    
        //2. Class member variables are multiple basic types
    class Employee{
        public string firstName;
        public string middleName;
        public string lastName;
        
        public string this[string index]{
            set{
                switch(index){
                    case "a":firstName = value;
                        break;
                    case "b":middleName = value;
                        break;
                    case "c":lastName = value;
                        break;
                    default: throw new ArgumentOutOfRangeException("index");
                }
            }
            get{
                switch(index){
                    case "a":return firstName;
                    case "b":return middleName;
                    case "c":return lastName;
                    default: throw new ArgumentOutOfRangeException("index");
                }
            }
        }
        
        static void Main(string[] args){
            Employee ee = new Employee();
            
            ee.firstName = "Hu";
            ee.middleName = "large";
            ee.lastName = "Yang";
            
            Console.WriteLine("My name is: {0}{1}{2}",ee["a"],ee["b"],ee["c"]);
        }
    }
}
103 original articles published, praised 7, visited 1520
Private letter follow

Posted on Tue, 14 Jan 2020 05:43:24 -0500 by sniperscope