using System;
namespace ConsoleApplication_C_Sharp
{
class Test
{
string[] _arr = new string[10];
public string this[int i]
{
get
{
if (i >= 0 && i < _arr.Length)
{
return _arr[i];
}
return "Index out of range: " + i;
}
set
{
if (i >= 0 && i < _arr.Length)
{
_arr[i] = value;
}
}
}
}
class Program
{
static void Main(string[] args)
{
Test t = new Test();
t[1] = "c#";
t[2] = "c";
t[3] = "c++";
t[4] = "java";
t[-1] = "abc";
t[11] = "xyz";
Console.WriteLine(t[1]);
Console.WriteLine(t[2]);
Console.WriteLine(t[3]);
Console.WriteLine(t[4]);
Console.WriteLine(t[-1]);
Console.WriteLine(t[11]);
}
}
}
/*
run:
c#
c
c++
java
Index out of range: -1
Index out of range: 11
*/