.Net, Programming

Use Indexer in C#

Indexer helps to make an interface to the class easy way and get so many benefits from that we will understand indexer with an example.

We are using classes with an aggregated or a composed collection of objects. For an example here, there are two classes Customer and Address.
Customer has addresses and so we have to initialize objects of address class and make a list of objects of address class.

public class Customer
{
private List<Address> AddressList = new List<Address>();

public Customer()
{
AddressList.Add(new Address
{
Street="Street1",
City = "City1",
State = "State1",
Pincode =442434
});

AddressList.Add(new Address
{
Street = "Street2",
City = "City2",
State = "State2",
Pincode = 442455
});

AddressList.Add(new Address
{
Street = "Street3",
City = "City3",
State = "State3",
Pincode = 532353
});

AddressList.Add(new Address
{
Street = "Street4",
City = "City4",
State = "State4",
Pincode = 523432
});

AddressList.Add(new Address
{
Street = "Street5",
City = "City5",
State = "State5",
Pincode = 623423
});
}

public Address GetAddress(string State)
{
foreach (Address addr in AddressList)
{
if (addr.State == State)
{
return addr;
}
}
return null;
}

public Address GetAddress(int Pincode)
{
foreach (Address addr in AddressList)
{
if (addr.Pincode == Pincode)
{
return addr;
}
}
return null;
}
}

public class Address
{
public string Street { get; set; }
public string State { get; set; }
public string City { get; set; }
public int Pincode { get; set; }
}

There are two methods to get address object by state or pin code so you can access it by GetAddress with pass parameters like below.

static void Main(string[] args)
{
Customer objCustomer = new Customer();
Address objAddress1= objCustomer.GetAddress("State1");
Address objAddress2 = objCustomer.GetAddress("442434");
}

Now we can simplify it by using indexer of the class.
Indexer starts with this keyword and pass parameter in it and also it always has got and set so you can get or set values. so we can write that GetAddress method like the below-using indexer.

public Address this[string State]
{
get{
foreach (Address addr in AddressList)
{
if (addr.State == State)
{
return addr;
}
}
return null;
}
}

public Address this[int Pincode]
{
get
{
foreach (Address addr in AddressList)
{
if (addr.Pincode == Pincode)
{
return addr;
}
}
return null;
}
}

The class itself uses indexes to get the object by simple as you can pass a parameter to the object so indexers are useful to make object accessible easy way.

static void Main(string[] args)
{
Customer objCustomer = new Customer();
Address objAddress1= objCustomer["State1"];
Address objAddress2 = objCustomer["442434"];
}

In many cases, the ‘index’ syntax makes a lot of sense, particularly useful if the SomeClass represents some sort of collection. We can see List<string>  where int as an indexer to get string object like List<string>[1].

Tagged

Leave a Reply

Your email address will not be published. Required fields are marked *