Signup/Sign In

C# Indexers and Properties

Posted in Programming   LAST UPDATED: SEPTEMBER 2, 2021

    This article introduces the concept of indexers and properties in C# programming language. Indexers enable a feature by which an object of a class can be indexed like an array, whereas properties enable to manage access to a class instance data. As you go through this article, you will come to know that, indexers and properties are related to each other, because both are dependent upon the C# accessor feature. At the end of this article, you will find the differences between indexers and properties.

    C# Indexers:

    An indexer is a smart array that enables an instance of a class or structure to be indexed like an array. Indexers must have at least one parameter else a compile-time exception will be generated. It is defined with this keyword and parameters, otherwise, it is the same as property. The syntax for one-dimensional indexer is shown here:

    <access_modifier> <return_type> this[parameters]
    {
        get
        {
            // return the value specified by index
        }
        set
        {
            // set a new value
        }
    }

    Let's see the brief description of each term

    access_modifier: It can be public, private, protected or internal.

    return_type: It can be any valid C# data type

    this: It is the keyword which points to the object of the current class

    parameters: This specifies the parameter list of the indexer

    get and set: These are the accessors

    Let's see an implementation of an indexer,

    Filename: Program.cs

    using System;
    
    namespace Studytonight
    {
        class Employee
        {
            private string[] arr = new string[2];
            public string this[int i]
            {
                get
                {
                    return arr[i];
                }
                set
                {
                    arr[i] = value;
                }
            }
        }
        public class Program
        {
            public static void Main(string[] args)
            {
                Employee eid = new Employee();
                eid[0] = "NOBANK129394940";
                eid[1] = "NOBANK129394941";
                for(int i=0; i<2; i++)
                {
                    Console.WriteLine(eid[i]);
                }
            }
        }
    }

    Output:

    NOBANK129394940
    NOBANK129394941

    In the above example, we have implemented an indexer in the Employee class. The class contains a private string array that external users can’t see. The indexer has a get and a set accessor method, where the get method is used to return indexer value whereas, the set method is used to assign a new value. In the set accessor, the value is a keyword through which the value gets assigned. Here, the Employee class behaves like a virtual array, and we accessed the class instance values using an array access operator ([ ]).


    C# Properties:

    The properties are also called as accessors as it is a member function of the class and provides a feature through which the values of a private field can be read, written, or changed. The property has two accessors, get and set, and these accessors helps to change the implementation of the class variables. The syntax for a property is shown here:

    <access_modifier> <return_type> <property_name>
    {
        get
        {
            // return the property value
        }
        set
        {
            // set a new value
        }
    }

    Let's see one practical example of properties

    Filename: Program.cs

    using System;
    
    namespace Studytonight
    {
        class Employee
        {
            private string eid = "NOBANK";
            public string Emp
            {
                get
                {
                    return eid;
                }
                set
                {
                    eid = value;
                }
            }
        }
    
        public class Program
        {
            public static void Main(string[] args)
            {
                Employee eid1 = new Employee();
                eid1.Emp = "NOBANK129394940";
                Console.WriteLine(eid1.Emp);
            }
        }
    }

    Output:

    NOBANK129394940

    In the above example, the eid variable is marked as private, and we are changing/assigning a new value using the property Emp. The Emp property has two accessors, get and set, the get accessor is used to return the property value, whereas set is used to assign a new value. In the main method, we have created the object of Employee class, and with the help of object and property Emp, we assigned the value.


    Difference between Indexers and Properties:

    Indexers Properties
    Indexers are created using this keyword Properties don't require this keyword
    Indexers are declared with at least one parameter Properties are always declared without parameters
    Indexers are accessed/invoked using indexes Properties are accessed/invoked using the specified name
    Indexers can't be static Properties can be declared as static
    Indexers can be overloaded Properties can't be overloaded


    Conclusion:

    We hope this article helped you to understand the indexers and properties in C# language. If you have any queries regarding this topic, then please let us know in the comment section below. We are happy to solve your doubts.

    You may also like:

    About the author:
    Subject Matter Expert of C# Programming at Studytonight.
    Tags:C#C# TutorialC# IndexersC# Properties
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS