In this program there is no call for test() method. Under these circumstances how it can output message (Using static constructor to initialize static data members ) in its content.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace static_eg
{
class Program
{
public class test
{
static string name;
static int age;
static test()
{
Console.WriteLine("Using static constructor to initialize static data members");
name = "John Sena";
age = 23;
}
public static void display()
{
Console.WriteLine("Using static function");
Console.WriteLine(name);
Console.WriteLine(age);
}
}
static void Main(string[] args)
{
test.display();
Console.ReadLine();
}
}
}
/*
Using static constructor to initialize static data members
Using static function
John Sena
23
*/
Method
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.

VulpesRank #55 · 29.2K reputationPosted Dec 4, 2011, 11:38 AM EST
It's called (once and once only) when a static member of the class is first accessed or the class is first instantiated.
In this program, it's triggered by the call to the static method test.display.
Posted Dec 4, 2011, 11:48 AM EST