TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Static Constructor In C#
Soumalya Das
Aug 24
2016
Code
906
0
2
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
namespace
Consolepractice
{
class
Program {
public
static
void
Main(
string
[] args) {
Test.x = 1;
//The static constructor for a class executes before any of the static members for the class are referenced.
Console.WriteLine(
"static member value:"
+ Test.x);
Test ob1 =
new
Test();
Test ob2 =
new
Test();
Console.WriteLine();
Console.ReadKey();
}
}
class
Test {
public
static
int
x;
public
Test()
// It will be executed every time when we create the object
{
Console.WriteLine(
"Default Constructor"
);
}
//There should at most one Static Constructor in a class and also Static Constructor can't take any parameters or access modifiers.
// It will be executed only once
//The static constructor for a class executes before any instance of the class is created.(before default constructor)
static
Test() {
Console.WriteLine(
"static Constructor"
);
}
}
}
// This is just a sample script. Paste your real code (javascript or HTML) here.
if
(
'this_is'
== /an_example/) {
of_beautifier();
}
else
{
var a = b ? (c % d) : e[f];
}
Static Constructor
C#