What is an Indexer? Explain how it is different from property in terms of implementation.

 

About an Indexer

An indexer provides array like syntax. It allows a class to be accessed the same way as an array is access means object of a class be indexed same way as an array. Modifier of an indexer can be of public, private, protected and internal.The return type can be any valid C# types.

Syntax of an Indexer

 
public string this[int parameter]{
   Get{	
	..code goes here
   }
   Set{
     ..code goes here
   }
}

So this is about an indexer. Now let go and see how an indexer is different from a property.

Difference Between Indexer and Property

1st Difference

Indexers are created by using "this" keyword and identified by its signature.

Properties don't use "this" keyword and identified by simple names.

For example

 
public string this[int parameter]{
   Get{	
	..code goes here
   }
   Set{
     ..code goes here
   }
}

Above snippet shows an indexer code with "string" as return type and followed by "this" keyword and "int" as input parameters.

 
public string EmployeeName{
   Get{	
	..code goes here
   }
   Set{
     ..code goes here
   }
}

Above snippet shows a property code with "string" as return type and followed by property name "EmployeeName".

2nd Difference

Indexers are accessed using indexes.

Properties are accessed by their names.

Indexer example

 
class clsEmployee{
	private string[] emplcodes = new string[10];
        public string this[int code]
        {
            get{
                return emplcodes[code];
            }
            set{
                emplcodes[code] = value;
            }
        }  
}
class Program{
  static void Main(string[] args){

            clsEmployee empl = new clsEmployee();
            empl[0] = "Shiv with Employee code";
            empl[1] = "Khadak with Employee code";
            empl[2] = "Shaam with Employee code";
   }

}

Above snippet shows a class "clsEmployee" which has got an indexer of string array values and in our main program method we have created object of a class and now indexers are accessed by class instance or object using an index value as shown in the above code.

Property Example

 
class clsEmployee{
	private string _emplname=””;
        public string EmployeeName
        {
            get{
                return _emplname;
            }
            set{
                _emplnam = value;
            }
        }  
}
class Program{

   static void Main(string[] args){

            clsEmployee empl = new clsEmployee();
            empl.EmployeeName  = “Shaam”;
   }

}

Above snippet shows a class "clsEmployee" which has got a property "EmployeeName" of string type and in our main program method we have created object of a class and now properties are accessed by class instance or object using a property name shown in the above code.

3rd Difference

A get and set accessors of an indexer has the same formal parameter list as the indexer.

A get accessor of a property has no parameters and a set accessor of a property contains the implicit value parameter.

This is all about an indexer if you have any doubts regarding this topic kindly let me know through your comments and if you like this article kindly share this with your friends,

 

 

 

:: SHARE THIS POST ::

        
 

More C# Articles To Read Next:

 
Jul
07
2013

Understanding Switch Statement with the help of an example

CLICK TO READ FULL ARTICLE

Views - 6326 |Category - C#

Jul
07
2013

Checked and Unchecked operators using an example

CLICK TO READ FULL ARTICLE

Views - 8231 |Category - C#

Jul
05
2013

What are structures? Differentiate between structures and classes

CLICK TO READ FULL ARTICLE

Views - 6626 |Category - C#

Jun
30
2013

Understanding the concept of pass by value and pass by reference with an example?

CLICK TO READ FULL ARTICLE

Views - 8152 |Category - C#

Jun
25
2013

Sealed Classes in C# with an example and explanation

CLICK TO READ FULL ARTICLE

Views - 32110 |Category - C#

 
Author: Gurunatha Dogi
is a software engineer by profession and founder of Onlinebuff.com, Onlinebuff is a tech blog which covers topics on .NET Fundamentals, Csharp, Asp.Net, PHP, MYSQL, SQL Server, Cell Phones and lots more. Follow me @ and twitter.
Leave a Comment......

All fields marked with * are mandatory

f9vni

 

 
Comments

Posted by Gopal on 2014-05-09

hello sir good tutorials and i want to become programmer in ,net what should i do