CSharp - What is the output: enum int value?

Question

What is the output of the following code?

using System;

class Program
{
    enum Values { val1, val2 = 26, val3 = 12, val4, val5 };
    static void Main(string[] args)
    {
        int x1 = (int)Values.val1;
        int x5 = (int)Values.val5;
        Console.WriteLine(x1);
        Console.WriteLine(x5);
    }
}


Click to view the answer

0
14

Note

Is the declaration valid?

enum Values { val1 = 7, val2 = 69, val3 = 75 };

We can assign explicitly to the members of the enumeration.

val1 is initialized with 0 by default.

But val3 is 12, so from there, values increased incrementally by 1.

Related Quiz