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
*/
Loading
Posted Dec 4, 2011, 4:48 PM
VulpesPosted Dec 4, 2011, 4:38 PM
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.