Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. /// <summary>
  5. /// Example of multiple indexers within a single class.
  6. ///
  7. /// Why you might want/need to do this, who knows?
  8. /// </summary>
  9.  
  10. namespace CSharp_Examples {
  11. /// <summary>
  12. /// Test object.
  13. /// </summary>
  14. public class Person {
  15. public int EmployeeID { get; set; }
  16. public int CitizenID { get; set; }
  17. public string Name { get; set; }
  18.  
  19. public Person(int empID, int citID, string name) {
  20. EmployeeID = empID;
  21. CitizenID = citID;
  22. Name = name;
  23. }
  24. }
  25.  
  26. /// <summary>
  27. /// Interface for explicit indexer usage.
  28. /// </summary>
  29. public interface ICitizen {
  30. Person this[int index] { get; }
  31. }
  32.  
  33. /// <summary>
  34. /// Interface for alternative explicit indexer.
  35. /// </summary>
  36. public interface IEmployee {
  37. Person this[int index] { get; }
  38. }
  39.  
  40. /// <summary>
  41. /// Object with a list of Person objects.
  42. /// </summary>
  43. public class Indexers : ICitizen, IEmployee {
  44. private List<Person> people = new List<Person>();
  45.  
  46. /// <summary>
  47. /// Standard indexer.
  48. /// </summary>
  49. /// <param name="index">Index in array of Person objects.</param>
  50. /// <returns></returns>
  51. public Person this[int index] {
  52. get {
  53. return people[index];
  54. }
  55. }
  56.  
  57. /// <summary>
  58. /// Add a Person object to the array.
  59. /// </summary>
  60. /// <param name="person"></param>
  61. /// <returns></returns>
  62. public Person Add(Person person) {
  63. int idx;
  64.  
  65. lock (people) {
  66. idx = people.FindIndex(p => p.EmployeeID == person.EmployeeID || p.CitizenID == person.CitizenID);
  67. if (idx >= 0) {
  68. throw new ArgumentException("Duplicate employee ID or citizen ID", "person");
  69. }
  70.  
  71. people.Add(person);
  72. }
  73.  
  74. return person;
  75. }
  76.  
  77. /// <summary>
  78. /// Indexer for the IEmployee interface. Gets an employee based on employee ID.
  79. /// </summary>
  80. /// <param name="employeeID"></param>
  81. /// <returns></returns>
  82. Person IEmployee.this[int employeeID] {
  83. get {
  84. return people.Find(p => p.EmployeeID == employeeID);
  85. }
  86. }
  87.  
  88. /// <summary>
  89. /// Indexer for ICitizen interface. Gets an employee based on citizen ID.
  90. /// </summary>
  91. /// <param name="citizenID"></param>
  92. /// <returns></returns>
  93. Person ICitizen.this[int citizenID] {
  94. get {
  95. return people.Find(p => p.CitizenID == citizenID);
  96. }
  97. }
  98. }
  99.  
  100. public class TestIndexers {
  101. public void Test() {
  102. Indexers persons = new Indexers();
  103.  
  104. persons.Add(new Person(1, 301938912, "Bob Smith"));
  105. persons.Add(new Person(3, 920001233, "Jane Doe"));
  106.  
  107. Person p;
  108.  
  109.  
  110. // Use ICitizen indexer to get person based on citizen ID.
  111. ICitizen c = persons;
  112. p = c[920001233];
  113.  
  114. // Use IEmployee indexer to get person based on employee ID.
  115. IEmployee e = persons;
  116. p = e[1];
  117.  
  118. // Returns null.
  119. p = ((IEmployee)persons)[2];
  120. }
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement