By 22/07/2015 14 Comments

Indexers In C#

  • Indexers in C# are class members which initializes special type of arrays.
  • Indexers implement objects as an arrays OR indexers enable objects to be indexed like arrays OR indexers break an object in different locations much like an array.
  • Like C# arrays indexers can have multiple dimensions such as 2D indexers, 3D indexers etc.
  • An indexer has not any user define name, that can be only accessed/call through the this keyword reference because an object reference variable automatically assigns to this keyword during runtime.
  • An indexer cannot be declared as static member of a class.
  • Like overloaded methods indexers can also be overloaded.
  • An indexer cannot contains null parameter, at least 1 or more parameters must be pass in an indexer.
  • An indexer has two keywords get and set which are called accessors where the set accessor initializes a private array with object reference values and get  accessor returns that array.
  • An indexer breaks an object data, re-arrange that data into different locations like an array through set accessor.

General Syntax:

access-modifier type this [parameterList]
{
      get { // code for get accessor }
      set { // code for set accessor }
}

STATEMENT DESCRIPTION
access-modifier It can be public, protected, private or internal.
type It defines a type of an indexer such as an int, string etc.
this An indexer has not any name, it is accessed through this keyword.
[parameterList] It describes the list of parameters to be passed in an indexer. Notice, that parameter list works as an index for indexer so it might be int types.
{ Starting scope or body of an indexer.
get { // code for get accessor } This is a get accessor which contains keyword return which returns an indexer.
set {// code for set accessor } This is a set accessor which has a keyword value which receives a value automatically and assigns or initializes a private array with object reference values. The value keyword works as a parameter.
} Closing scope or body of an indexer.

Example:

using System;
namespace csharpBasic
{
    class Technology
    {
        // String type single-dimensional array declaration with private access.
        private string[] ProgramingLanguages = new string[6];

        /* Read only property declaration (IndexerLength) of an int type 
           which returns the length an array (ProgramingLanguages).*/
        public int IndexerLength
        {
            get
            {
                return ProgramingLanguages.Length;
            }
        }

        // One-dimensional string type indexer declaration.
        public string this[int locationIndex]
        { // start an indexer scope.

            get
            { // get accessor returns an array field.
                return ProgramingLanguages[locationIndex];
            }

            set
            { // set accessor sets or assigns an object reference values to an array.
                ProgramingLanguages[locationIndex] = value;
            }
        } // End of indexer scope.
    }

    class Program
    {
        static void Main(string[] args)
        {
            // An object creation.
            Technology technology = new Technology();

            // Different string types values are being assigned into each object's index.
            technology[0] = "Asp.net";
            technology[1] = "Css3";
            technology[2] = "Html5";
            technology[3] = "TypeScript";
            technology[4] = "Php";
            technology[5] = "Jquery";

            // for loop loops through each array elements/values and display them.
            for (int countIndex = 0; countIndex < technology.IndexerLength; countIndex++)
                Console.WriteLine(technology[countIndex]);

            Console.ReadKey();
        }
    }
    /*  The Output will be:
     
        Asp.net
        Css3
        Html5
        TypeScript
        Php
        Jquery
     */
}





Next Tutorial →


← Previous Tutorial


Posted in: C# Advance, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .