Created
April 19, 2017 22:12
-
-
Save Banane9/cd14b40123d3a9e78f6e38d5f79a2bb4 to your computer and use it in GitHub Desktop.
How to implement smart "enums" (like in Java) in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class SmartDayOfTheWeek | |
{ | |
public static SmartDayOfTheWeek Monday { get; } = new Monday(); | |
public static SmartDayOfTheWeek Tuesday { get; } = new Tuesday(); | |
// Etc... | |
public static IEnumerable<SmartDayOfTheWeek> Days | |
{ | |
get | |
{ | |
yield return Monday; | |
yield return Tuesday; | |
// Etc... | |
} | |
} | |
public abstract string Name { get; } | |
public abstract int Number { get; } | |
private SmartDayOfTheWeek() | |
{ } | |
private sealed class Monday | |
{ | |
public override string Name { get; } = "Monday"; | |
public override int Number { get; } = 0; | |
} | |
private sealed class Tuesday | |
{ | |
public override string Name { get; } = "Tuesday"; | |
public override int Number { get; } = 1; | |
} | |
// Etc... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment