Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of Indexers?

Tags:

c#

What is the real use of indexer in C#?

It looks pretty confusing as I am a new c# programmer. It looks like an array of objects but it's not.

like image 455
Embedd_0913 Avatar asked Mar 04 '09 07:03

Embedd_0913


People also ask

What is indexer with example?

An indexer is a special type of property that allows a class or a structure to be accessed like an array for its internal collection. C# allows us to define custom indexers, generic indexers, and also overload indexers. An indexer can be defined the same way as property with this keyword and square brackets [] .

What is difference between indexers and array?

Fundamentally, an indexer is a pair of methods (or a single one) whereas an array access gives you a storage location. Note that if you weren't using a mutable struct to start with, you wouldn't see the difference in this way - I'd strongly advise against using mutable structs at all.

Which among the following are the advantages of using indexers?

Which among the following are the advantages of using indexers? Explanation: Indexers provides a view at large scale to visualize a collection of items as an array. It is really easy to use the object of the class that represents a collection as if it is an array.


2 Answers

You can think of an indexer as a Property that takes a parameter. What you do with this parameter is up to you.

Generally, the indexers are used where providing , well, an index to your object makes sense.

For instance, if your object deals with collections of items, being able to access them using a simple '[]' syntax makes sense because it's simple to think of it as an array to which we give the position of the object we want to return.

I wrote a blog entry a while ago on the aspects of indexers using reflection.
One of the examples I gave was:

 using System;  
 using System.Collections;  

 public class DayPlanner  
 {  
     // We store our indexer values in here   
     Hashtable _meetings = new System.Collections.Hashtable();  

     // First indexer  
     public string this[DateTime date] {  
         get {  
             return _meetings[date] as string;  
         }  
         set {  
             _meetings[date] = value;  
         }  
     }  

     // Second indexer, overloads the first  
     public string this[string datestr] {  
         get {  
             return this[DateTime.Parse(datestr)] as string;   
         }  
         set {  
             this[DateTime.Parse(datestr)] = value;  
         }  
     }  
 }  

And then you could use it like this:

  DayPlanner myDay = new DayPlanner();  
  myDay[DateTime.Parse("2006/02/03")] = "Lunch";  
  ...  
  string whatNow = myDay["2006/06/26"];

That's just to show you can twist the purpose of an indexer to do what you want.
Doesn't mean you should though... :-)

like image 162
Renaud Bompuis Avatar answered Oct 07 '22 21:10

Renaud Bompuis


Indexers allow instances of a class or struct to be indexed just like arrays, they are most frequently implemented in types whose primary purpose is to encapsulate an internal collection or array.

A get accessor returns a value and a set accessor assigns a value:

public T this[int i]
{
    get
    {
        return arr[i];
    }
    set
    {
        arr[i] = value;
    }
}

With the enums, they defined values are limited to a small set of primitive integral types (byte, sbyte, short, ushort, int, uint, long, ulong).

The char type is also a integral type, but it differs from the other integral types in two ways:

  • There are no implicit conversions from other types to the char type. In particular, even though the sbyte, byte, and ushort types have ranges of values that are fully representable using the char type, implicit conversions from sbyte, byte, or ushort to char do not exist.

  • Constants of the char type must be written as character-literals or as integer-literals in combination with a cast to type char. For example, (char)10 is the same as '\x000A'.

(C# Language Specification 3.0, Section 4.1.5)

like image 29
Christian C. Salvadó Avatar answered Oct 07 '22 23:10

Christian C. Salvadó