Is there some way I can use ConsoleColors as the Ints that they are? Like
Console.ForeGroundColor = 10; //ConsoleColor.Green has the Value of 10
but i can only use
Console.ForeGroundColor = ConsoleColor.Green; //when hovering over "Green" Visual Studio even shows me that Green is 10
I managed to find these Color-Ints in the registry (HKEY_CURRENT_USER/Console) where the Ints have hexCodes of the colors but no where the name (and i was able to change the value of Green to Orange and back to default but that doesn't matter). So why won't C# or Visual Studio let me use the Ints? Or am I doing something Wrong?
Please try to explain it without using refering to enums. I know, these colornames are enums but i just don't understand enum-conversion yet
ConsoleColor
is enum
, not int, And you cannot implicitly convert type int
to it.
You can use type casting:
int x = 10;
Console.ForegroundColor = (ConsoleColor)x;
Or
Console.ForegroundColor = (ConsoleColor)10;
Cast int to enum in C#
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/enumeration-types
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With