Тип индексатора: объяснить работу кода - C#

Узнай цену своей работы

Формулировка задачи:

Здравствуйте ! Подскажите почему в строке : public int this[string day] тип индексатора и параметра разные int и string . Программа рабочая.
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Использование__индексаторов_1
  6. {
  7. // Using a string as an indexer value
  8. class DayCollection
  9. {
  10. string[] days = { "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };
  11. // This method finds the day or returns -1
  12. private int GetDay(string testDay)
  13. {
  14. for (int j = 0; j < days.Length; j++)
  15. {
  16. if (days[j] == testDay)
  17. {
  18. return j;
  19. }
  20. }
  21. throw new System.ArgumentOutOfRangeException(testDay, "testDay must be in the form "Sun", "Mon", etc");
  22. }
  23. // The get accessor returns an integer for a given string
  24. public int this[string day]
  25. {
  26. get
  27. {
  28. return (GetDay(day));
  29. }
  30. }
  31. }
  32. class Program
  33. {
  34. static void Main(string[] args)
  35. {
  36. DayCollection week = new DayCollection();
  37. System.Console.WriteLine(week["Fri"]);
  38. // Raises ArgumentOutOfRangeException
  39. System.Console.WriteLine(week["Sun"]);
  40. // Keep the console window open in debug mode.
  41. System.Console.WriteLine("Press any key to exit.");
  42. System.Console.ReadKey();
  43. }
  44. }
  45. }
  46. // Output: 5

Решение задачи: «Тип индексатора: объяснить работу кода»

textual
Листинг программы
  1. private int GetDay(string testDay)

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

8   голосов , оценка 4.25 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы