Difference between private constructor and static constructor

Posted by Karthik2010_Mca under C# on 3/26/2013 | Points: 10 | Views : 19568 | Status : [Member] | Replies : 5
Hi,

Can you please explain anyone,
Difference between private constructor and static constructor in C#?

Thanks

Karthik


Responses

Posted by: Murugavelmsc on: 3/26/2013 [Member] Starter | Points: 25

Up
0
Down
Static Constructor

Used to initialize the static members of a class.
Can not access non-static members.
Executes before the first instance of a class. We can not determine the time of execution.
Executes by the CLR not by the object of a class.
There are no parameterized static constructors since it is handled by the CLR not by the object.
Time of execution might be at the loading of contained assembly.


Private Constructor

Used to restrict a class to be instantiated and to be inherited.
Used whenever a class contains only static members.

Regards,
Murugavel S
murugavel.sadagopan@gmail.com
http://murugavelmsc.blogspot.in/

Karthik2010_Mca, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Murugavelmsc on: 3/26/2013 [Member] Starter | Points: 25

Up
0
Down
http://www.technolatte.net/2011/07/private-and-static-constructors-in-c.html

Regards,
Murugavel S
murugavel.sadagopan@gmail.com
http://murugavelmsc.blogspot.in/

Karthik2010_Mca, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 3/26/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi
refer this
http://www.c-sharpcorner.com/interviews/answer/3902/

Mark as Answer if its helpful to you

Karthik2010_Mca, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sandeepmhatre on: 3/26/2013 [Member] Starter | Points: 25

Up
0
Down

Refer this link

Constructor in detail,

http://sandeepmhatre.blogspot.com/2013/03/constructors.html

Sandeep M,
Software Developer
Follow me on :
http://sandeepmhatre.blogspot.in

Karthik2010_Mca, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Senthilstayss on: 3/28/2013 [Member] Starter | Points: 25

Up
0
Down
1. Once you have the private constructor inside the class ,then you can't create the instance for it..You can go with private constructor if suppose you want to initialize the Static fields or Const fields from some external resource.

2.Static constructors used to initialize the class level static variables. Static constructors are called only once throughout the life time of the application.i.e when the first instance of class having static constructor is created or when we access or use the static fields first time.

Note: Static constructors can't have access modifiers and arguments.

Senthil D

Karthik2010_Mca, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response