Created
December 6, 2018 22:36
-
-
Save aalmada/6e0e05c39d99f4c1aeacdd3fdf57e32c to your computer and use it in GitHub Desktop.
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 enum MyEnum : short | |
{ | |
First = 1, | |
Second = 2, | |
Third = 3 | |
} |
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 enum MyEnum | |
{ | |
First, | |
Second, | |
Third | |
} |
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 enum MyEnum : short | |
{ | |
First = 1, | |
Second = 2, | |
Third = 3, | |
AnotherThird = 3, | |
First = 3 // compilation error | |
} |
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 enum ColorName | |
{ | |
Red = 1, | |
Green, | |
Blue, | |
} | |
public static class MyClass | |
{ | |
public static Color GetColor(ColorName color) | |
{ | |
switch (color) | |
{ | |
case ColorName.Red: | |
return Color.FromRgb(255, 0, 0); | |
case ColorName.Green: | |
return Color.FromRgb(0, 255, 0); | |
case ColorName.Blue: | |
return Color.FromRgb(0, 0, 255); | |
default: | |
throw new Exception("Unexpected color name!"); | |
} | |
} | |
} |
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
MySmartEnum.First.Equals(MySmartEnum.First); // = true | |
MySmartEnum.First.Equals(MySmartEnum.Third); // = false | |
MySmartEnum.Third.Equals(MySmartEnum.AnotherThird); // = true |
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 sealed class MySmartEnum : SmartEnum<MySmartEnum, short> | |
{ | |
public static readonly MySmartEnum First = | |
new MySmartEnum("A string.", 1); | |
public static readonly MySmartEnum Second = | |
new MySmartEnum("Another string.", 2); | |
public static readonly MySmartEnum Third = | |
new MySmartEnum("Yet another string.", 3); | |
private MySmartEnum(string name, short value) | |
: base(name, value) {} | |
} |
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 sealed class MySmartEnum : SmartEnum<MySmartEnum> | |
{ | |
public static readonly MySmartEnum First = | |
new MySmartEnum(nameof(First), 1); | |
public static readonly MySmartEnum Second = | |
new MySmartEnum(nameof(Second), 2); | |
public static readonly MySmartEnum Third = | |
new MySmartEnum(nameof(Third), 3); | |
private MySmartEnum(string name, int value) | |
: base(name, value) {} | |
} |
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 sealed class MySmartEnum : SmartEnum<MySmartEnum> | |
{ | |
public static readonly MySmartEnum First = | |
new MySmartEnum(nameof(First), 1); | |
public static readonly MySmartEnum Second = | |
new MySmartEnum(nameof(Second), 2); | |
public static readonly MySmartEnum Third = | |
new MySmartEnum(nameof(Third), 3); | |
public static readonly MySmartEnum AnotherThird = | |
new MySmartEnum(nameof(AnotherThird), 3); | |
public static readonly MySmartEnum First = | |
new MySmartEnum(nameof(First), 3); // runtime error | |
private MySmartEnum(string name, int value) | |
: base(name, value) {} | |
} |
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 static Color GetColor(ColorName name) | |
{ | |
if (name is null) | |
throw new ArgumentNullException(nameof(name)); | |
switch (name.Name) | |
{ | |
case nameof(ColorName.Red): | |
return 1; | |
case nameof(ColorName.Red): | |
return 2; | |
case nameof(ColorName.Red): | |
return 3; | |
default: | |
throw new Exception("Unexpected value!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment