By 22/07/2015 8 Comments

C# Indexers In Interface

  • In C#, like methods and properties an interface can also define the indexers.
  • Indexers in an interface have not their body, they have only declaration.

OR

  • Indexers in an interface have not complete implementation, they only have their declaration.

Example:

using System;
namespace csharpBasic
{
    // An interface declaration.
    interface ITechnology
    {
        // An indexer declaration.
        string this[int locationIndex]
        {
            get;
            set;
        }
    }

    // Technology class implements ITechnology interface.
    class Technology : ITechnology
    {
        // string type instance array field with private access.
        private string[] ProgramingLanguages = new string[6];

        /* Read only property declaration (IndexerLength) 
          of an int type which returns the length of an array. */
        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 declaration.

                ProgramingLanguages[locationIndex] = value;
            }
        } // End of an indexer scope.
    }

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

            // Sring values are assigned in each indexer location.
            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 .