fork download
  1. using System;
  2.  
  3. class Layout
  4. {
  5. string[] _values = new string[100]; // Backing store
  6.  
  7. public string this[int number]
  8. {
  9. get
  10. {
  11. // This is invoked when accessing Layout instances with the [ ].
  12. if (number >= 0 && number < _values.Length)
  13. {
  14. // Bounds were in range, so return the stored value.
  15. return _values[number];
  16. }
  17. // Return an error string.
  18. return "Error";
  19. }
  20. set
  21. {
  22. // This is invoked when assigning to Layout instances with the [ ].
  23. if (number >= 0 && number < _values.Length)
  24. {
  25. // Assign to this element slot in the internal array.
  26. _values[number] = value;
  27. }
  28. }
  29. }
  30. }
  31.  
  32. class Program
  33. {
  34. static void Main()
  35. {
  36. // Create new instance and assign elements
  37. // ... in the array through the indexer.
  38. Layout layout = new Layout();
  39. layout[1] = "Frank Gehry";
  40. layout[3] = "I. M. Pei";
  41. layout[10] = "Frank Lloyd Wright";
  42. layout[11] = "Apollodorus";
  43. layout[-1] = "Error";
  44. layout[1000] = "Error";
  45.  
  46. // Read elements through the indexer.
  47. string value1 = layout[1];
  48. string value2 = layout[3];
  49. string value3 = layout[10];
  50. string value4 = layout[11];
  51. string value5 = layout[50];
  52. string value6 = layout[-1];
  53.  
  54. // Write the results.
  55. Console.WriteLine(value1);
  56. Console.WriteLine(value2);
  57. Console.WriteLine(value3);
  58. Console.WriteLine(value4);
  59. Console.WriteLine(value5); // Is null
  60. Console.WriteLine(value6);
  61. }
  62. }
Success #stdin #stdout 0.02s 33896KB
stdin
Standard input is empty
stdout
Frank Gehry
I. M. Pei
Frank Lloyd Wright
Apollodorus

Error