Hello, I know that console.foregroundColor takes a ConsoleColor enum as a parameter. What I do not get is why - I guess these are strings so when console.foreground recieve it as an argument, it will use some kind of switch based on these constants. Is it right? Or is there something I have missed?
+2
A:
The Console only supports 16 colors. I can only assume the enum is directly mapped to native constants, thus it does not require a switch.
leppie
2010-10-14 09:03:48
Yes, but if it could take string (like "Black") as a parameter, it would be the same, right? I mean, if enum only contains list of elements, then the console.foreground has to distinct somehow which was means what.
Ptr
2010-10-14 09:08:07
@Ptr, see my answer. As leppie points out, as actual enum value maps to native constant, there is no need for switch statement. Enums are kind of type-safe named integer constants.
VinayC
2010-10-14 09:08:23
+1
A:
Enum has integral values and actual ConsoleColor values are 0, 1, 2, 3 and so on. Further, as leppie points out ConsoleColor values actually mapped to native color values. In the native color value for console - Foreground values go into lower four bits while background values go into upper 4 bits. Enum provides ease of use and hence framework library had wrapped console colors as enum.
VinayC
2010-10-14 09:07:50
I do not understad what do you mean "mapped to native colors". If each element has its intergral type (its like index?), then what is that mapping?
Ptr
2010-10-14 09:11:17
For example, value of ConsolColor.DarkBlue is 1 and corresponding value of constant (in windows api) FOREGROUND_BLUE is also 1. See http://msdn.microsoft.com/en-us/library/ms682093(VS.85).aspx and http://msdn.microsoft.com/en-us/library/ms682088(v=VS.85).aspx#_win32_character_attributes to understand win api structures that are involved here.
VinayC
2010-10-14 10:19:27