Skip to content

Instantly share code, notes, and snippets.

@aalmada
Created December 6, 2018 22:36
Show Gist options
  • Save aalmada/6e0e05c39d99f4c1aeacdd3fdf57e32c to your computer and use it in GitHub Desktop.
Save aalmada/6e0e05c39d99f4c1aeacdd3fdf57e32c to your computer and use it in GitHub Desktop.
public enum MyEnum : short
{
First = 1,
Second = 2,
Third = 3
}
public enum MyEnum
{
First,
Second,
Third
}
public enum MyEnum : short
{
First = 1,
Second = 2,
Third = 3,
AnotherThird = 3,
First = 3 // compilation error
}
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!");
}
}
}
MySmartEnum.First.Equals(MySmartEnum.First); // = true
MySmartEnum.First.Equals(MySmartEnum.Third); // = false
MySmartEnum.Third.Equals(MySmartEnum.AnotherThird); // = true
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) {}
}
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) {}
}
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) {}
}
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