C
C#2y 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
Aaron2y ago
(MyEnum)someByte
MODiX
MODiX2y 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
Aaron2y ago
oh you have a byte? what do you want to happen when it's null a null reference exception? some default value?
Jwan
JwanOP2y ago
null reference exception
Aaron
Aaron2y ago
(MyEnum)(object)nullableByte or (MyEnum)nullableByte.Value if you're okay with InvalidOperationException instead
Jwan
JwanOP2y ago
Thank you ❤️
Tvde1
Tvde12y ago
(MyEnum?) nullableByte To cast it to a nullable version of your enum
Accord
Accord2y 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