fork(1) download
  1. using System;
  2.  
  3. class FailSoftArray {
  4. int[] a; // ссылка на базовый массив
  5. // Построить массив по заданному размеру.
  6. public FailSoftArray(int size)
  7. {
  8. a = new int[size];
  9. Length = size;
  10. }
  11. // Автоматически реализуемое и доступное только для чтения свойство Length.
  12. public int Length
  13. {
  14. get; private set;
  15. }
  16. // Автоматически реализуемое и доступное только для чтения свойство Error.
  17. public bool Error
  18. {
  19. get; private set;
  20. }
  21. // Это индексатор для массива FailSoftArray.
  22. public int this[int index]
  23. {
  24. // Это аксессор get.
  25. get
  26. {
  27. if (ok(index))
  28. {
  29. Error = false;
  30. return a[index];
  31. }
  32. else
  33. {
  34. Error = true;
  35. return 0;
  36. }
  37. }
  38. // Это аксессор set.
  39. set
  40. {
  41. if (ok(index))
  42. {
  43. a[index] = value;
  44. Error = false;
  45. }
  46. else Error = true;
  47. }
  48. }
  49. // Возвратить логическое значение true, если
  50. // индекс находится в установленных границах.
  51. private bool ok(int index)
  52. {
  53. if (index >= 0 & index < Length) return true;
  54. return false;
  55. }
  56. }
  57.  
  58. public class Test
  59. {
  60. public static void Main()
  61. {
  62. FailSoftArray fs = new FailSoftArray(5);
  63. // Использовать свойство Error.
  64. for (int i = 0; i < fs.Length + 1; i++)
  65. {
  66. fs[i] = i * 10;
  67. Console.WriteLine(fs[i]);
  68. if (fs.Error)
  69. Console.WriteLine("Ошибка в индексе " + i);
  70. }
  71. }
  72. }
Success #stdin #stdout 0.02s 14456KB
stdin
Standard input is empty
stdout
0
10
20
30
40
0
Ошибка в индексе 5