Indexers Vs Properties in C# (C Sharp)

Indexers and Properties are instance members of a class. Both have similar purpose but its usage and accessibility is different. This article does a compare and contrast on Indexers and Properties. To understand about these two concepts, a simple example is given below. The example is then followed by the illustration and comparison.


Example:

class declareIndexerClass {
public string sampleProperty;
public string sampleArray[] = new string[3];
public string SampleProperty {
get {return sampleProperty;}
set { sampleProperty = value;}
}
public string this[int indexVal] {
get {
if(indexVal >=0 && indexVal <3) {
return sampleArray[indexVal];
}
else {
Console.WriteLine(“Array Index is Out of Bounds”);
}
}
set {
if(indexVal >=0 && indexVal <3) {
sampleArray[indexVal] = value;
}
else {
Console.WriteLine(“Array Index is Out of Bounds”);
}

}
}
}

public class testIndexerClass {
public static void Main( )
{
//Accessing Property
declareIndexerClass sampleObj = new declareIndexerClass();
sampleObj.sampleProperty = “SAMPLE”;
console.WriteLine(sampleObj.sampleProperty);

//Accessing Indexer
sampleObj[0] = “0th element”;
sampleObj[1] = “1st element”;
sampleObj[2] = “2nd element”;
console.WriteLine("{0}\n,{1}\n,{2}\n,{3}\n",sampleObj[0],sampleObj[1],
sampleObj [2]);

}
}


Illustration of Example:

This example uses both property and indexer. The class declareIndexerClass contains two properties namely sampleProperty and sampleArray. The later is a one dimensional array. sampleProperty has its accessor methods (get and set) defined. Followed by this definition is an indexer which is used to get and set values of sampleArray.

Indexer accepts indexVal as parameter and this is used as index to get and set a particular array element. Validation is done to check if the index is valid. If this condition check is not done and an incorrect index is used then IndexOutOfRangeException will be thrown.

This situation is avoided using if-else condition checks inside the ‘get’ and ‘set’ method of indexer. ‘get’ method fetches an array element located by specific index and ‘set’ method sets the specific array element identified by the index. testIndexerClass is another class that has its Main method defined. Inside this method, an instance of declareIndexerClass is created. Using this instance, both the property and the indexer are accessed.

Comparison of Property and Indexer:

In this section, you will do a comparison of property and indexer using the example discussed above.

• Property of a class should have a unique name. Indexer doesn’t require a name, and it is accessed using “this”. In the above example, sampleProperty is a property. Indexer is defined using the following line of code:
public string this[int indexVal]

• Class can have any number of properties and each property can have two accessor methods namely:
• get – For fetching value of the property
• set – For setting value for the property
Indexers can also have ‘get’ and ‘set’ access methods. If any of the property or indexer is read-only then it will only have the ‘get’ access method and ‘set’ method will not be defined.

• ‘get’ access method of property accepts has no parameters. But ‘get’ access method of indexer accepts the parameters passed along with “this” pointer.

• ‘set’ access method of property can only use the implicit parameter called value. Consider the following statement,
set { sampleProperty = value;}
This line of code uses value parameter. ‘set’ access method of indexer can use value parameter as well as all other parameters defined along with the indexer. In the above example, ‘set’ method of indexer uses indexVal as well as value parameter.

• Property of a class will be accessed as objectname.propertyname. Indexers are accessed only using the object name. In the above example, property is accessed as:

sampleObj.sampleProperty = “SAMPLE”;
Indexer is accessed as:
sampleObj[0] = “0th element”;

This is because an indexer doesn’t have a name and it is defined using “this” which refers to the instance directly. In this example, indexers are used to fetch and store elements of sampleArray. If indexers are not used, then sampleObj.sampleArray[0] has to be used instead of sampleObj[0] to access 0th element of the array.

• Properties cannot have parameters associated with it. But indexers should be associated with parameters. It is mandatory that indexer has to be associated with at least one parameter. More parameters can also be included. In this example,

public string this[int indexVal]

Means that indexVal is a parameter associated with indexer. Unlike methods where parameters are included using ( ) brackets, in indexers parameters are mentioned inside [ ] brackets.

• Properties can be static. But indexers cannot be static as they have to be accessed only using the instances.

_______________________________________________________________________

Add to My Yahoo!


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource

|Designing applications in .Net using Service Oriented Architecture | How to Create and Use Anonymous Delegates in C# (C Sharp) |How to Define Custom Attributes in C# (C Sharp)| How To Handle Errors While Developing ASP.NET Applications | How to Use Indexer in Classes and Interfaces of C# (C Sharp)| Illustration of Access Keywords (base, this) with Examples in C# (C Sharp) | Illustration of Null Coalesce Operator (??) in C# (C Sharp) |Indexers Vs Properties in C# (C Sharp) | More about Reserved Attributes of C# (C Sharp)| Overview of Unified Type System of C# (C Sharp) | Purpose of Delegates in C# (C Sharp) |How does Simple Object Access Protocol (SOAP) help in Internet Communication |How to Create and Use Anonymous Delegates in C# (C Sharp)|

 


| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.