But if you don't become bogged down minutiae at the expense of your project, how can you ever call yourself a REAL programmer? 
Exactly! Now...
Here is what you need to know. ConsoleColor is neither a primitive,
nor is it a class, but something else. In truth it is a lot closer to an int than anything else, but you can think of it as a super buffed up bool, and it is called an Enum. Short of enumerator. These things can be very handy.
Think of a bool, it only has two possible states, true and false. No middle ground, no margin for error. Imagine we are programming for a shop that sells shirts, and they only sell shirts in two sizes, small and large. We could easily use a bool to show shirt size like this.
class Shirt
{
private bool isLarge;
public bool IsLarge
{
get
{
return isLarge;
}
}
}
Now all large shirts are shown with true, all small shirts are false, we can tell one from the other, job well done!
But then a darkness. The boss thinks that we should
also sell medium sized shirts. MADNESS!!! A bool can not hold three different sorts of values! He doesn't seem to care for these details, and tells you to make it work.
Well there are several ways to do this, we could decide to use an int instead, make small = 0, medium = 1 and large = 2, but then we need to fuck around with meanless numbers the entire program. Really fucking annoying. Instead, we can use an enum! That way, small = small, medium = medium and large = large. For example
enum ShirtSize
{
Small,
Medium,
Large
}
class Shirt
{
private ShirtSize size;
public ShirtSize Size
{
get
{
return size;
}
}
}
You see how we make an enum in a similar way to a class? Similar idea, but instead of 'class
name {}' we have 'enum
name {}'
Also, our lines do
not end with a semicolon, rather a ','
This is because we are not declaring process, or a field, but a type. The compiler doesn't go over these like lines, so it doesn't have to know when a line ends.
We can use enums in comparative statements
class Program
{
static void Main(string[] args)
{
Shirt shirt = new Shirt(ShirtSize.Small);
if (shirt.Size == ShirtSize.Large)
{
Console.WriteLine("This should fit you buddy!");
}
else
{
Console.WriteLine("Sorry, you will have to visit a gym.");
}
}
}
And also switch statements and loops, with similar syntax. As you can imagine, they make life tolerable when you have these massive if statements and have to try and remember what each and every int means! Instead, you have a descriptive name.