Основы объектно-ориентированного программирования на языке C# book | Page 129
7.5 Iндексатори
using System;
namespace TestIndexer
{
class ElevatorWithIndexer
{
public int height;
public int width;
public int length;
public int capacity;
private string[] floor;
private uint AmmountOfFloor;
public ElevatorWithIndexer(uint Count)
{
// опис конструктора
floor = new string[Count];
AmmountOfFloor = Count;
height = 2;
width = 3;
length = 1;
capacity = 800;
}
public uint Size
{
// отримання розмiру масиву
get
{
return AmmountOfFloor;
}
}
public string this[uint index]
{
// опис iндексатора
get
{
if (index > -1 && index < Size)
return floor[index]; // отримання елементу
else return "Wrong number of floor";
}
set
{
floor[index] = value;
// встановлення значення
}
}
public void Up()
{
Console.WriteLine("Up");
}
public void Down()
{
Console.WriteLine("Down");
}
}
class Program
{
static void Main(string[] args)
{
ElevatorWithIndexer MyElevator = new ElevatorWithIndexer(3);
MyElevator[0] = "first";
129