What is the output of the program below?

Technology CommunityCategory: C#What is the output of the program below?
VietMX Staff asked 3 years ago
Problem

Consider:

public class TestStatic {
 public static int TestValue;

 public TestStatic() {
  if (TestValue == 0) {
   TestValue = 5;
  }
 }
 static TestStatic() {
  if (TestValue == 0) {
   TestValue = 10;
  }

 }

 public void Print() {
  if (TestValue == 5) {
   TestValue = 6;
  }
  Console.WriteLine("TestValue : " + TestValue);

 }
}

public void Main(string[] args) {

 TestStatic t = new TestStatic();
 t.Print();
}

TestValue : 10

The static constructor of a class is called before any instance of the class is created. The static constructor called here initializes the TestValue variable first.