Guest User

Untitled

a guest
Aug 19th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. How can you override an enum in C#?
  2. void Main()
  3. {
  4. StarTrek baseClass = new StarTrek();
  5. NewGeneration childClass = new NewGeneration();
  6.  
  7. baseClass.ShowEnum();
  8. childClass.ShowEnum();
  9. }
  10.  
  11. public class StarTrek
  12. {
  13. internal enum Characters
  14. {
  15. Kirk, Spock, Sulu, Scott
  16. }
  17.  
  18. public void ShowEnum()
  19. {
  20. Type reflectedEnum = typeof(Characters);
  21. IEnumerable<string> members = reflectedEnum.GetFields()
  22. .ToList()
  23. .Where(item => item.IsSpecialName == false)
  24. .Select(item => item.Name);
  25. string.Join(", ", members).Dump();
  26. }
  27. }
  28.  
  29. public class NewGeneration : StarTrek
  30. {
  31. internal new enum Characters
  32. {
  33. Picard, Riker, Worf, Geordi
  34. }
  35. }
  36.  
  37. public class StarTrek
  38. {
  39. internal virtual IList<string> Characters
  40. {
  41. get
  42. {
  43. return new List<string> { Kirk, Spock, Sulu, Scott };
  44. }
  45. }
  46.  
  47. public void ShowEnum()
  48. {
  49. string.Join(", ", Characters).Dump();
  50. }
  51. }
  52.  
  53. public class NewGeneration : StarTrek
  54. {
  55. internal override IList<string> Characters
  56. {
  57. get
  58. {
  59. return new List<string> { Picard, Riker, Worf, Geordi };
  60. }
  61. }
  62. }
Add Comment
Please, Sign In to add comment