Open In App

Difference Between Properties and Indexers in C#

Last Updated : 24 Jul, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Properties in C# are named members that use access modifiers to set and retrieve values of fields declared in a secured manner. Properties are used for abstracting and encapsulating access to a field of a class by defining only important actions and hiding their implementation. Properties are invoked through a described name and can be declared as a static or an instance member.

Difference-Between-Properties-and-Indexers-in-CSharp

Syntax of declaring a property in C#:

[access_modifier] [return_type] [PropertyName]  
{
//body of property
}

Indexers in C# are data members that act as an array and allow you to access data within objects to be indexed in the same way. Indexers are always declared as instance members, never as static members. Indexers are implemented in the same way as properties, except that the declaration of an indexer must have at least one parameter.

Syntax of creating an indexer in C#:

[access_modifier] [return_type] this [parameter]
{
  get 
  {
     // return value
  }
  set 
  {
    // return value
  }
}

Difference between Properties and Indexers in C#

Properties Indexers
1. Properties are declared by giving a unique name. Indexers are declared without giving a name.
2. Properties are identified by the names While indexers are identified by the signatures.
3. Properties can be declared as a static or an instance member. Indexers are always declared as instance member, never as static member.
4. Properties are invoked through a described name. Indexers are invoked using an index of the created object.
5. Properties does not needs this keyword in their creation. Indexers needs this keyword in their keyword.
6. A get accessor of a property does not have any parameters. A get accessor of a property contains the list of same proper parameters as indexers.

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads