Understand Indexer in C#

Sourav.Kayal
Posted by in C# category on for Beginner level | Points: 250 | Views : 2717 red flag

How to use Indexer in C# class.

Understand Indexer in C#

In this tutorial we will learn the concept of Indexer in C#. The formal definition of Indexer is “Indexer allows an object to be indexed like an array”. If we define indexer in one class then the class will behave like virtual array. We can access the object of that class just like array. In this article we will try to understand how indexer works.  Have a look on below code.

Indexer in class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace Client
{
    class Person
    {
        public string Name { get; set; }
        Person[] p = new Person[10];
       
        public Person this[int i]
        {
            get
            {
                return p[i];
            }
            set
            {
                p[i] = value;
            }
        }
    }
   
    class Program
    {
        static void Main(string[] args)
        {
            Person per = new Person();
 
            per[0] = new Person();
            per[0].Name = "Sourav Kayal";
            Console.WriteLine(per[0].Name);
            Console.ReadLine();
        }
    }
}

Output is given below.

 

In this example we have define simple Indexer in Person class.  We are seeing that with the help of indexer we can able to use the object of Person class just like array with the help of [] operator.

Access string using Indexer

In this example we will see how to access string array with the help of Indexer.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace Client
{
    class Person
    {
        string[] name =  { "Ram", "Shyam", "Sourab" };
        public string this[int i]
        {
            get
            {
                return name[i];
            }
            set
            {
                name[i] = value;
            }
        }
    }
   
    class Program
    {
        static void Main(string[] args)
        {
            Person p = new Person();
            Console.WriteLine("0 th name=" +   p[0]);
            Console.WriteLine("1 st name=" + p[1]);
            Console.ReadLine();
        }
    }
}

Here is sample output.  


Here we are accessing string array with the help of Person class just like array. We can implement Indexer even in Generic class. In below example we will implement in generic class.

Indexer in generic class.

We can implement indexer in generic class. In below example we will implement indexer in generic class. Try to understand below code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Collections;
using Client;
namespace Client
{
    class Test<T>
    {
 
        private T[] p = new T[10];
        public T this[int i]
        {
            get
            {
                return p[i];
            }
            set
            {
                p[i] = value;
            }
        }
  
    }
   
    class Program
    {
        static void Main(string[] args)
        {
            Test<string> strs = new Test<string>();
            strs[0] = "Sourav Kayal";
            Console.WriteLine("With String:= " +  strs[0]);
 
            Test<int> sint = new Test<int>();
            sint[0] = 100;
            Console.WriteLine("With Integer" + sint[0]);
            Console.ReadLine();
        }
    }
}

Here is output.


Conclusion:-

In this article we have learned how to use Indexer in C# class. Hope you understood the concept.  

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)