1
enum Days { Sun, Mon, Tue, Wed, Thu, Fri, Sat }; 

static void Main(string[] args) {
    int x = (int)Days.Tue;  
    Console.WriteLine(x);   //Outputs 2
}

I have two questions:

Q1. I don't understand why we need to assign int x to Enum as Enum have default values starting with index 0?

Q2. What is the real life example usage of Enum?

Guy
  • 46,488
  • 10
  • 44
  • 88
  • 2
    You don't assign `x` to an enum, you assign the enum value to `x`. – Guy Oct 17 '19 at 06:45
  • In many cases, you are not intereseted in the integer value of the enum. You would write `Days myDay = Days.Tue; Console.WriteLine(myDay); // Outputs Tue` – SomeBody Oct 17 '19 at 06:45
  • 4
    Possible duplicate of [What are enums and why are they useful?](https://stackoverflow.com/questions/4709175/what-are-enums-and-why-are-they-useful) – Vivek Nuna Oct 17 '19 at 06:47
  • `What is the real life example usage of Enum?` In short: better readability like in your example for 'weekdays'. – nilsK Oct 17 '19 at 06:59

1 Answers1

0

Enums enforce-ish* valid values.

enum Days { Sun, Mon, Tue, Wed, Thu, Fri, Sat }; 

static void Main(string[] args) {
    int weekday1 = (int)Days.Tue;  

    weeekday1 = 33; // What does this mean?

    Days weekday2 = Days.Tue;

    weekday2 = 33; // Compilation error. Good.
}

* = It's quite easy to make enum's value any int value, if you really want to.

tymtam
  • 31,798
  • 8
  • 86
  • 126