using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConstructorCallingStaticAndPublicConstructor
{
class Program
{
static Program()
{
Console.WriteLine("static constructor called");
}
public Program()
{
Console.WriteLine("public constructor called");
}
static void Main(string[] args)
{
Program p = new Program();
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConstructorCallingStaticAndPublicConstructor
{
class Program
{
static Program()
{
Console.WriteLine("static constructor called");
}
public Program()
{
Console.WriteLine("public constructor called");
}
static void Main(string[] args)
{
Program p = new Program();
Console.ReadLine();
}
}
}
output:-
static constructor called
public constructor called
Flow of execution:-
Once class is loaded into memory by CLR then static constructor will be called automatically at only once.
now control of execution moves to main method and execute Program p=new Program();
at this point public constructor will be called.
Note: if comment Program p=new Program() line
then only one static contructor will be called when code will be run.
No comments:
Post a Comment