C
C#15mo ago
Jwan

❔ How do I cast a byte to an enum MyEnum : byte

As the title says really, I have a public enum MyEnum : byte and I want to cast byte? to the enum. How do I do that? 🤔
8 Replies
Aaron
Aaron15mo ago
(MyEnum)someByte
MODiX
MODiX15mo ago
Windows10CE#8553
REPL Result: Success
enum MyEnum : byte { A, B, C }

byte b = 1;
Console.WriteLine((MyEnum)b);
enum MyEnum : byte { A, B, C }

byte b = 1;
Console.WriteLine((MyEnum)b);
Console Output
B
B
Compile: 578.446ms | Execution: 76.761ms | React with ❌ to remove this embed.
Aaron
Aaron15mo ago
oh you have a byte? what do you want to happen when it's null a null reference exception? some default value?
Jwan
Jwan15mo ago
null reference exception
Aaron
Aaron15mo ago
(MyEnum)(object)nullableByte or (MyEnum)nullableByte.Value if you're okay with InvalidOperationException instead
Jwan
Jwan15mo ago
Thank you ❤️
Tvde1
Tvde115mo ago
(MyEnum?) nullableByte To cast it to a nullable version of your enum
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts