Thursday, October 25, 2012

.NET Indexer program examples


Indexer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Indexers
{
    class Samp
    {

        int[] x;
        public Samp()
        {
            x = new int[] { 1, 2, 3, 4, 5, 6 };
        }

        public int this[int i]
        {
            get
            {
                return x[i];
            }
            set
            {
                x[i] = value;
            }
        }

        public static void Main(string[] args)
        {
            Samp s = new Samp();
            Console.WriteLine("The third element is ");
            Console.WriteLine("x[3]={0}", s[3]);
            Console.ReadLine();
        }
    }

}

Output
The third element is
x[3]=4





No comments:

Post a Comment