Friday, 1 September 2017

C# programming tutorial to implement the use of Indexers

C# tutorial programming | C# program to implement the use of Indexers

Program

using System; class Number
    {
        int[] a = new int[4];  // Array Declaration
        public int this[int index] // Indexer Declaration
        {
            get   // Getter Method (Output Defining)
            {
                return a[index];
            }
            set   // Setter Method (Input and Assigning Values)
            {
                a[index] = value;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Number n = new Number();             // Array and Indexer Instantiation
            n[0] = 1;
             n[1] = 2;
             n[2] = 3;
             n[3] = 4;
            Console.WriteLine("The Index Values are:");
            for (int i = 0; i < 4; i++)
            {
                Console.WriteLine("Value at Index[{0}] : {1}", i, n[i]);
            }
        }

    }

Output


0 comments:

Post a Comment