Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- How can you override an enum in C#?
- void Main()
- {
- StarTrek baseClass = new StarTrek();
- NewGeneration childClass = new NewGeneration();
- baseClass.ShowEnum();
- childClass.ShowEnum();
- }
- public class StarTrek
- {
- internal enum Characters
- {
- Kirk, Spock, Sulu, Scott
- }
- public void ShowEnum()
- {
- Type reflectedEnum = typeof(Characters);
- IEnumerable<string> members = reflectedEnum.GetFields()
- .ToList()
- .Where(item => item.IsSpecialName == false)
- .Select(item => item.Name);
- string.Join(", ", members).Dump();
- }
- }
- public class NewGeneration : StarTrek
- {
- internal new enum Characters
- {
- Picard, Riker, Worf, Geordi
- }
- }
- public class StarTrek
- {
- internal virtual IList<string> Characters
- {
- get
- {
- return new List<string> { Kirk, Spock, Sulu, Scott };
- }
- }
- public void ShowEnum()
- {
- string.Join(", ", Characters).Dump();
- }
- }
- public class NewGeneration : StarTrek
- {
- internal override IList<string> Characters
- {
- get
- {
- return new List<string> { Picard, Riker, Worf, Geordi };
- }
- }
- }
Add Comment
Please, Sign In to add comment