Friday, August 9, 2019

C# indexers

C# indexers are usually known as smart arrays. A C# indexer is a class property that allows you to access a member variable of a class or struct using the features of an array. In C#, indexers are created using this keyword. Indexers in C# are applicable on both classes and structs. 
Instances of that class can be accessed using the [] array access operator.

Important points to remember on indexers: 

  • Indexers are always created with this keyword.
  • Parameterized property are called indexer.
  • Indexers are implemented through get and set accessors for the [ ] operator.
  • ref and out parameter modifiers are not permitted in indexer.
  • The formal parameter list of an indexer corresponds to that of a method and at least one parameter should be specified.
  • Indexer is an instance member so can't be static but property can be static.
  • Indexers are used on group of elements.
  • Indexer is identified by its signature where as a property is identified it's name.
  • Indexers are accessed using indexes where as properties are accessed by names.
  • Indexer can be overloaded.

Indexer are defined in pretty much same way as properties, with get and set functions. The main difference is that the name of the indexer is the keyword this.

Difference between Indexers and Properties

Indexers
Properties
Indexers are created with this keyword.
Properties don't require this keyword.
Indexers are identified by signature.
Properties are identified by their names.
Indexers are accessed using indexes.
Properties are accessed by their names.
Indexer are instance member, so can't be static.
Properties can be static as well as instance members.
A get accessor of an indexer has the same formal parameter list as the indexer.
A get accessor of a property has no parameters.
A set accessor of an indexer has the same formal parameter list as the indexer, in addition to the value parameter.
A set accessor of a property contains the implicit value parameter.


Indexers are commonly used for classes, which represents some data structure, an array, list, map and so on.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp4
{
    public class Customer
    {
        private List<Address> Addresses = new List<Address>();
        public Customer()
        {
            Addresses.Add(new Address { Pincode = 110059, Mobile = "9999999999" });
            Addresses.Add(new Address{ Pincode = 110064, Mobile = "8888888888" });
        }
        public Address GetAddress(int Pincode)
        {
            foreach (var item in Addresses)
            {
                if (item.Pincode == Pincode) return item;
            }
            return null;
        }
        //Indexer property
        public Address this[int Pincode]
        {
            get
            {
                foreach (var item in Addresses)
                {
                    if (item.Pincode == Pincode) return item;
                }
                return null;
            }
        }
    }
    public class Address
    {
        public int Pincode { get; set; }
        public string Mobile { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Customer cust = new Customer();
            //Address addr= cust.GetAddress(110059);  //calling the method through class object
            Address addr = cust[110059];  // we have used indexer on class itself instead of calling the properties through class object
            Console.WriteLine(addr.Mobile);
            Console.ReadKey();

        }
    }
}

No comments:

Post a Comment