---
title: "Indexer in C# with example"  
description: "The indexer is a class property that’s treated an object as an array. Or in other word indexer is a class property that’s access a class or structure"  
author: "Hemant Patel"  
published: 2017-03-08  
updated: 2018-03-17  
canonical: https://www.mindstick.com/blog/11315/indexer-in-c-sharp-with-example  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Indexer in C# with example

The indexer is a class [property](https://www.mindstick.com/blog/205/property-notification-in-c-sharp) that’s treated an object as an array. Or in other word indexer is a class property that’s access a class or [structure](https://www.mindstick.com/articles/23258/choose-your-business-structure-wisely) in the same way as arrays.When you define an indexer for a class, then this class behaves similar to [virtual](https://yourviews.mindstick.com/view/81341/the-oh-so-fight-for-democracy-virtual-rally-scenes) array. You also access the [instance](https://www.mindstick.com/forum/1739/object-reference-is-not-set-to-an-instance-of-an-object-error-in-my-code) of an object of this class using array access [operator](https://www.mindstick.com/blog/144/union-intersection-and-except-operator-in-sql-server) []. The indexer is also similar to a property. You use get and set accessors for defining an indexer. Indexer defines with ‘this’ keyword. It's not defined with names.\

\

\

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Collections; namespace Learn{    public class Program    {        public static void Main()        {            StringDataStore stringStore = new StringDataStore();            stringStore [0] = "First";            stringStore [1] = "Second";            stringStore [2] = "Third";            stringStore [3] = "Fourth";            for (int i = 0; i < 10; i++)                Console.WriteLine(stringStore [i]);            Console.ReadLine();        }    }     public class StringDataStore    {        private string[] stringArr = new string[10]; // internal data storage        public StringDataStore()        {         }         public string this[int indx]        {            get            {                if (indx < 0 && indx >= stringArr.Length)                    throw new IndexOutOfRangeException("Cannot store more than 20 bjects");                 return stringArr [indx];            }                        set            {                if (indx < 0 && indx >= stringArr.Length)                    throw new IndexOutOfRangeException("Cannot store more than 20 bjects");                 stringArr [indx] = value;            }        }    }}
```

**You can visit useful related post**

[Indexer in C#](https://www.mindstick.com/Articles/11962/indexer-in-c-sharp)

[What are indexers in C# .NET?](https://www.mindstick.com/Interview/2167/what-are-indexers-in-c-sharp-dot-net)

[Is it possible to have a static indexer in C#?](https://www.mindstick.com/Interview/375/is-it-possible-to-have-a-static-indexer-in-c-sharp)

[what is difference between indexers and properties?](https://www.mindstick.com/interview/335/what-is-difference-between-indexers-and-properties)

---

Original Source: https://www.mindstick.com/blog/11315/indexer-in-c-sharp-with-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
