User Name:


User Email:




This information will only be saved for the purposes of communicating with those who have provided this information voluntarily regarding our services.We will never sell your name or email address to anyone.
© 2018 - First Crazy Developer (Abhishek Kumar)
  

crazydeveloper What are Indexers in C# ?

In c# introduce new concept is Indexer. This is very useful for some situation. Let as discuss something about Indexer.

  • Indexer Concept is object act as an array.
  • Indexer an object to be indexed in the same way as an array.
  • Indexer modifier can be private, public, protected or internal.
  • The return type can be any valid C# types.
  • Indexers in C# must have at least one parameter. Else the compiler will generate a compilation error.

In previous statements you knew that what is Indexers. Now we will discuss that what exactly are Indexer and in which scenario we need Indexers. Many times classes have contained  (aggregate or composed) collection.

In real scenario  there is two class

public class Customer{...}

public class Address{...}


So a customer can have multiple address.

public class Address
{
    public int Pincode { get; set; }
    public string Address { get; set; }
    public string Mobile { get; set; }
}


 public class Customer

{

    private List<Address> AddressList = new List<Address>(); 

//This is aggregated collection under in Customer class and most important that we declare this as private to implement encapsulation.

 

    public Customer()

    {

        AddressList.Add(new Address { Pincode = 110062, Address = "South Delhi", Mobile = "9871062500" });

        AddressList.Add(new Address { Pincode = 110076, Address = "North Delhi", Mobile = "7533019330" });

        AddressList.Add(new Address { Pincode = 110045, Address = "East Delhi", Mobile = "98710654321" });

        AddressList.Add(new Address { Pincode = 110023, Address = "West Delhi", Mobile = "9871082565" });

        AddressList.Add(new Address { Pincode =  110019, Address = "Center Delhi", Mobile = "9871062786" });

    }


There is two scenario come in End user part to get Customer on base of address like Pincode and Mobile...


Normally what we do we define two function with overloading

 public Address getAddress(int pincode)
    {
        foreach(Address add in AddressList)
        {
            if (add.Pincode == pincode)
                return add;
        }
        return null;
    }

******************************************************************************************
    public Address getAddress(string mobile)
    {
        foreach (Address add in AddressList)
        {
            if (add.Mobile == mobile)
                return add;
        }
        return null;
    }

}


When we call with parameter it will iterate in Address list collection and if data found then return that.

So question come into mind that how can we simplify that this process, we don't want to call  function, we just want to get customer details on address properties  base. How can we do that? To resolve this problem Indexers come into scene.


Indexers help us to simplify the way to access a collection for a class.

Summarizing in a sentence -  Indexers help to access contained collection  with in a class using a simplified  interface. It's a syntactic sugar.

Here sample code to create Indexer :

 public Address this[int pincode]
    {
      
  get
        {
           
foreach(Address add in AddressList)

        {
            if (add.Pincode == pincode)
                return add;
        }
        return null;

        }

    }

*********************************************************************************
    public Address this[string mobile]
    {
        get
        {
            foreach (Address add in AddressList)
        {
            if (add.Mobile == mobile)
                return add;
        }
        return null;

        }


******************************************************************************

Now we can access this Indexer like this :

public class User
{
    Customer Customers = new Customer();
    public User()
    {
        Address add = Customers[110062];
        
    }
}

Now in below image that when you use class through index it provide you details that you can access that through pincode

In .NetFramework lots of place internally Indexer used like List etc.


There are two things we need to remember about Indexer :-

  •  "Indexer" is defined by "this" keyword
  •   Indexer is property so we need to define set and get for the same.


crazydeveloper Home Page 13 June 2015

Become a Fan