Тип индексатора: объяснить работу кода - C#
Формулировка задачи:
Здравствуйте ! Подскажите почему в строке : public int this[string day]
тип индексатора и параметра разные int и string . Программа рабочая.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Использование__индексаторов_1
{
// Using a string as an indexer value
class DayCollection
{
string[] days = { "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };
// This method finds the day or returns -1
private int GetDay(string testDay)
{
for (int j = 0; j < days.Length; j++)
{
if (days[j] == testDay)
{
return j;
}
}
throw new System.ArgumentOutOfRangeException(testDay, "testDay must be in the form "Sun", "Mon", etc");
}
// The get accessor returns an integer for a given string
public int this[string day]
{
get
{
return (GetDay(day));
}
}
}
class Program
{
static void Main(string[] args)
{
DayCollection week = new DayCollection();
System.Console.WriteLine(week["Fri"]);
// Raises ArgumentOutOfRangeException
System.Console.WriteLine(week["Sun"]);
// Keep the console window open in debug mode.
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
}
// Output: 5Решение задачи: «Тип индексатора: объяснить работу кода»
textual
Листинг программы
private int GetDay(string testDay)