Difference Between Properties and Indexers in C#

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.
My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.