Google News
logo
C# - Interview Questions
What are Indexers in C#?
C# introduces a new concept known as Indexers which are used for treating an object as an array. The indexers are usually known as smart arrays in C#. They are not an essential part of object-oriented programming.
 
Defining an indexer allows you to create classes that act as virtual arrays. Instances of that class can be accessed using the [] array access operator.
 
Creating an Indexer 
< modifier > <    
return type > this[argument list] {    
    get {    
        // your get block code    
    }    
    set {    
        // your set block code    
    }    
} ​
   
In the above code,
 
<modifier>
 
can be private, public, protected or internal.
 
<return type>
 
can be any valid C# types.
Advertisement