CSharp - What is the output: enum circular definition?

Question

What is the output of the following code?

using System;
class Program
{
    enum Values { val1 = 12, val2 = (int)TrafficLight.green, val3, val4 = 200 };
    enum TrafficLight : byte
    {
        red, green = (int)Values.val3, yellow
    };

    static void Main(string[] args)
    {
        Console.WriteLine("** A simple use of enum ***");
        foreach (Values v in Enum.GetValues(typeof(Values)))
        {
            Console.WriteLine("{0} is storing {1}", v, (int)v);
        }
    }
}


Click to view the answer

Compilation error.

Note

We are actually supplying a circular definition.

val2 is getting values from TrafficLight.green. val3 is dependent on that value since it will be incremented by 1.

TrafficLight.green is dependent on val3.

Related Quiz