Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I want to know whether my understanding on the constructors is correct or not. I just tried to attempt a quiz for constructors but confused whether my understanding is correct or not

--> Every class needs at least one constructor defined - False (In C#, if you don't provide one, the compiler automatically provides a default constructor.)

--> It’s possible to avoid a class being instantiated by simply having only a private constructor on it. – False (A private constructor is a special instance constructor. It is generally used in classes that contain static members only. In a nutshell, you can still create an instance of a class with private constructor thru some other means (e.g. from static methods)

--> A static constructor in a non­static class is called once on each object creation. – False (If a static constructor has been called once, compiler will not call it again, it will get called once only .The execution of a static constructor is triggered by the first of the following events to occur within an application domain: 1. An instance of the class type is created. 2. Any of the static members of the class type are referenced.)

-->You can specify on the constructor which classes of interfaces that class is inheriting
( i don't know please clarify )
Updated 27-Dec-13 21:31pm

1 solution

False - you specify interfaces and the base class on the class definition, not on the constructor.
Share this answer
Member 10491035 28-Dec-13 2:50am    
Thank you for answering. Is my understanding on the other statements of constructor correct?
OriginalGriff 28-Dec-13 3:57am    
The first one is badly worded - but you are broadly correct.
The second you also correct - the only way to prevent a class being instantiated is not to use it!
The third you also correct - calling a static constructor twice would kinda defeat the idea... :laugh:

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