An example. Here we see a variable initializer. When you have a field in a class, you can assign that field directly inside the class declaration.
Program We see the Test class, which uses a variable initialization. The Program class provides Main().
First The Test class is constructed and allocated upon the managed heap. The constructor calls GetPi and stores its result.
GetPi This method call was inserted at the top of the constructor in the compiled code.
C# program that uses variable initializer in class
string _value = Program.GetPi();
// Simple constructor statements.
Console.WriteLine("Test constructor: " + _value);
public static string GetPi()
// This method returns a string representation of PI.
static void Main()
// Create an instance of the Test class.
Test test = new Test();
Test constructor: 3.14159265358979
Order. Let's inspect the .ctor() member. We see that the initialization of the field _value was inserted at the top of the constructor body.