using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IndexerProgram
{
class Program
{
static void Main(string[] args)
{ int choice = 5;
IndexerOfSecond ios = new IndexerOfSecond();
int currentIndex = 0;
do
{
Console.WriteLine("Choose one of the following\n");
Console.WriteLine("1. Enter value\n2.Get element\n3.Exit\n");
Console.WriteLine("Write Now:");
int ch = int.Parse(Console.ReadLine());
if(ch == 1)
{
Console.WriteLine("\nEnter the value to store : ");
int val = int.Parse(Console.ReadLine());
ios[currentIndex] = new Second(val);
currentIndex++;
}
if(ch == 2)
{
Console.WriteLine("\nEnter the Index to get : ");
int index = int.Parse(Console.ReadLine());
(ios[index]).printVal();
}
if (ch > 2 || ch < 1)
{
Environment.Exit(0);
}
Console.WriteLine("\n Do you want more operations . Press 5\n");
choice = int.Parse(Console.ReadLine());
} while (choice == 5);
}
}
class Second
{
int val;
public Second ()
{
val=0;
}
public Second(int x)
{
val = x;
}
public void printVal()
{
Console.WriteLine("\nValue is : " + val + "\n");
}
}
class IndexerOfSecond
{
private readonly Second[] SecondObjArray = new Second[10];
public static int size = 10;
public Second this[int index]{
get
{
if(index > (size-1) && index < 0)
{
return new Second();
}
else
{
return SecondObjArray[index];
}
}
set
{
if (index <= 9 && index >-1)
{
SecondObjArray[index] = value;
}
}
}
}
}
Indexer in C#
