Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# use ConsoleColor as int [duplicate]

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

like image 895
fly_over_32 Avatar asked Mar 04 '23 18:03

fly_over_32


1 Answers

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

like image 78
BDF Avatar answered Mar 16 '23 00:03

BDF