Skip to content
Related Articles

Related Articles

C# – Indexers Using String as an Index
  • Difficulty Level : Medium
  • Last Updated : 30 Aug, 2020

Prerequisite: Properties in C#

Indexers allow instances of a class or struct to be indexed just like arrays. By using indexers class will behave like a virtual array. The indexed value can be set or retrieved without explicitly specifying a type or instance member. Indexers resemble properties except that their accessors take parameters. Indexers are almost similar to the Properties. The main difference between Indexers and Properties is that the accessors of the Indexers will take parameters.

Indexers using a string as an index:

This method will give you more information, readability. If we want to retrieve information using a string as an index.

Example:

ic["username"] = "user12";  
ic["password"] = "12345";  

This will give more readability than the code given below.

ic[0] = "user12";  
ic[1] = "12345";  

Syntax:



[access_modifier] [return_type] this [argument_list]
{
 get  
 {
    // retrieval code or code to get the value 
 }
 set  
 {
   // code for setting value to member
 }

In the above syntax:

  • access_modifier: It can be public, private, protected, or internal.
  • return_type: It can be any valid C# type. In this case, it will be of string type.
  • this: It is the keyword which points to the object of the current class.
  • argument_list: This specifies the parameter list of the indexer.
  • get{ } and set { }: These are the accessors.

Example:

C#




// C# program to illustrate the Indexers 
// Using String as an Index 
using System; 
  
// class declaration 
class IndexerPOC 
  
    // class members 
    private string[] val = new string[4]; 
    
    // indexer array 
    private string[] indices={"username","password","email","Book"};
  
    // Indexer declaration 
    // Here pubic is the modifier 
    // string is the return type of 
    // Indexer and "this" is the keyword 
      // which refer to the calling object.
    // having parameters list 
    public string this[string index] 
    
  
        // get Accessor 
        // retrieving the values 
        // stored in val[] array 
        // of strings using string indexer.
        get
        
  
            return val[Array.IndexOf(indices,index)]; 
        
  
        // set Accessor 
        // setting the value at 
        // passed i of val 
        set
        
  
            // value keyword is used 
            // to define the value 
            // being assigned by the 
            // set indexer. 
            val[ Array.IndexOf(indices,index)] = value; 
        
    
  
// Driver Class 
class Program { 
      
    // Main Method 
    public static void Main() { 
          
        // creating an object of parent class which 
        // acts as primary address for using Indexer 
        IndexerPOC ic = new IndexerPOC(); 
  
        // Inserting values in ic[] 
        // Here we are using the object 
        // of class as an array 
        ic["username"] = "user12"
        ic["password"] = "12345"
        ic["email"] = "user123@gmail.com"
        ic["Book"]="CSHARP";
        Console.Write("Printing values stored in objects used as arrays\n"); 
          
        // printing values 
        Console.WriteLine("UserName = {0}", ic["username"]); 
        Console.WriteLine("Password = {0}", ic["password"]); 
        Console.WriteLine("Email = {0}", ic["email"]); 
        Console.WriteLine("Book = {0}", ic["Book"]); 
      
    
}

Output:

Printing values stored in objects used as arrays
UserName = user12
Password = 12345
Email = user123@gmail.com
Book = CSHARP

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :