Static variable order

.net, c#, static-variables

Solution

The static fields are initialized in the same order as the declarations. When you initialize `v2` with the value of `v1`, `v1` is not initialized yet, so its value is 0.

Problem

I have aproblem with the order of static variable declaration in C# When i run this code: ``` static class Program { private static int v1 = 15; private static int v2 = v1; static void Main(string[] args) { Console.WriteLine("v2 = "+v2); } } ``` The output is: ``` v2=15 ``` But when i change the static variable declaration order like this: ``` static class Program { private static int v2 = v1; private static int v1 = 15; static void Main(string[] args) { Console.WriteLine("v2 = "+v2); } } ``` The Output is: ``` v2 = 0 ``` Why this happend?

Original source

Related problems