Click here to Skip to main content
16,020,974 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How to access the private constructor class even it is not inherited nor instantiated.

If we are having static methods in the private constructor class and accessed by its class name. Then what the static constructor signifies?

Is it possible without static method in private constructor class can we access members
in the class.

What I have tried:

I have a little confusion in that when I used to face the interview.
Posted
Updated 6-Apr-16 20:50pm
Comments
Philippe Mori 6-Apr-16 23:33pm    
Static and private comstructors are not related at all. Static constructor is called once before first use of class. Private constructor are nor callable from outside the class.

I would suggest you to read documentation...
Sergey Alexandrovich Kryukov 6-Apr-16 23:51pm    
Correct. This is the same as asking "what's the difference between apple and crocodiles?" (Or apple and Apple :-)
However, the documentation may not clarify such delicate matter as static constructors, so I answered the question in Solution 1.

On second though... I looked through the reference page — it's good enough. I just got a habit not to trust the MSDN descriptions of the deep fundamentals, they are usually not comprehensively explained and sometimes misleading. Examples: delegates, events, thread initialization, anonymous delegate instance construction, and more...

—SA

Please check below url. this will help you

Constructor in C Sharp[^]
 
Share this answer
 
As almost all "what is the difference" question, this one is totally incorrect. Those notions are not directly related. Private is about accessibility, static vs instance is per-class vs per instance. Just read about constructors and you will see.

As static non-private constructor would make no sense at all, the syntax prohibit static constructors to be non-private, but this is the only reason, there is nothing fundamental in it.

So, learn two separate thing: access specifiers and their meaning (private, protected, internal, internal protected and public) and what does "static" means.

For methods, "static" means having no implicit parameter "this" passed to all instance methods. This is true even for constructors: in contrast to C++, .NET constructors work with "almost constructed" object and can operate any instance members in its implementation through "this" (explicitly or implicitly used), and also static members.

Naturally, a static constructor does not have access to any instance members, because there is no instance (yet). It can work even before application entry point method (Main) — this is a popular tricky question. This is a very interesting piece in general. The code of static constructor is needed for preparation work which is related to the class itself, not any of the class instances. For example, it's used to initialize some instance data members. Therefore, the runtime system should guarantee that such constructor is called before the very first instance of the class is created.

Interestingly, it's no true that static constructors are not related to class instances at all, because it won't be called if the class is not used during runtime at all. Any use of any static members of instance members (a use of instance members cannot happen without constructing at least one instance) trigger the call of a static constructor before the use. Naturally, this is also true for derived classes, if the static constructor is in a base class.

It's very important to understand all this background if static constructors have to be used.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900