Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

C# - Static Constructor in C#.NET with Example

Sep 27, 2013
Introduction:

Here I will explain what is static constructor in c# with example. Static constructor in c# is used to create static fields of the class and to write the code that needs to be executed only once. 

Description:


Constructor is a special method of a class which will invoke automatically whenever instance or object of class is created. Constructors are responsible for object initialization and memory allocation of its class. If we create any class without constructor, the compiler will automatically create one default constructor for that class. There is always at least one constructor in every class. If you want to know more about constructors check this article constructors in c#.

Static Constructor

When we declared constructor as static it will be invoked only once for any number of instances of the class and it’s during the creation of first instance of the class or the first reference to a static member in the class. Static constructor is used to initialize static fields of the class and to write the code that needs to be executed only once.


using System;
namespace ConsoleApplication3
{
class Sample
{
public string param1, param2;
static Sample()
{
Console.WriteLine("Static Constructor");
}
public Sample()
{
param1 = "Sample";
param2 = "Instance Constructor";
}
}
class Program
{
static void Main(string[] args)
{
// Here Both Static and instance constructors are invoked for first instance
Sample obj=new Sample();
Console.WriteLine(obj.param1 + " " + obj.param2);
// Here only instance constructor will be invoked
Sample obj1 = new Sample();
Console.WriteLine(obj1.param1 +" " + obj1.param2);
Console.ReadLine();
}
}
}
When we run above program we will get output like as shown below

Output


Static Constructor
Sample Instance Constructor
Sample Instance Constructor
Importance points of static constructor

-      Static constructor will not accept any parameters because it is automatically called by CLR.
-      Static constructor will not have any access modifiers.
-      Static constructor will execute automatically whenever we create first instance of class
-      Only one static constructor will allowed.

I hope it helps to know about static constructor. If you want to know more about constructors check this article constructors in c#.

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

11 comments :

Anonymous said...

static constructor will be invoked only once for any number of instances of the class are created .

devtools korzh said...

Nice tips, will come at handy for me!

Unknown said...

Thanks a lot..U really deserve a much much more than a great round of applause..Im sorry i cant give much more than that..

Revathy said...

Super,Poweful,Clearcut explanation.

Thank U so much Suresh Ji :)

Anonymous said...

Helped me a lot :))))) thank u :)))

Thomas Reuiters interview questions c#.Net said...

thanks nice article

Unknown said...

Real time use ? Sir

Anonymous said...

You should give more explanation of static constructor..How about using it inside abstract class ..if there is a static constructor in base class how can we use the values in derived class..also what about the practical scenerio where it is used..these days people asked more where it is used and y will u use..This u will anywhere .....

Unknown said...

static constructor is use to initialize any static datd

Anonymous said...

Thank u so much guiding us in a right direction.

Gaurav said...

nice article!

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.