What are Indexers in C#?

Category: C SharpWhat are Indexers in C#?
Editor">Editor Staff asked 2 years ago

What are Indexers in C#?

1 Answers
Editor">Editor Staff answered 2 years ago

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 

 

  1. < modifier > <    
  2. return type > this[argument list] {    
  3.     get {    
  4.         // your get block code    
  5.     }    
  6.     set {    
  7.         // your set block code    
  8.     }    
  9. }    

In the above code,
 
<modifier>
 
can be private, public, protected or internal.
 
<return type>
 
can be any valid C# types.