6

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

  • I know, i can make a ConsoleColor[] Array and loop through that. – fly_over_32 Jan 07 '19 at 10:58
  • @Wayne Phipps yes i have seen this question. But i am relativly new to programming and i have no idea what an enum is – fly_over_32 Jan 07 '19 at 11:35
  • An **enumeration** defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code.. and yes, I am Jonathan :D – Jonathan Jan 07 '19 at 12:11

2 Answers2

6

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

BDF
  • 86
  • 4
2

I use Vs 2017: I wrote

Console.ForegroundColor = (ConsoleColor)10;
Console.ForegroundColor = (ConsoleColor)12;

and worked !

CodeMan
  • 671
  • 6
  • 13