c# (Csharp) interview questions :- What is the use of private constructor ?

30
20



For more such videos visit
For more such videos subscribe

See our other Step by Step video series below :-
Learn C# Step by Step
Learn Angular tutorial step by step
Learn MVC Core step by step :-
Learn Design Pattern Step by Step
Learn Data Science in 1 hour :-
Learn Power BI Step by Step:-
Learn MSBI Step by Step in 32 hours:-
Learn Tableau step by step :-
Learn SQL Server Step by Step
Learn SharePoint Step by Step in 8 hours:-

Want to learn C# start from this video
This is one of those c# (Csharp) and .NET interview question is asked from the perspective to test if you understand the importance of private constructors in a class.
When we declare a class constructor as private , we can not do 2 things:-
— We can not create a object of the class.
— We can not inherit the class.
For more such videos visit

We are also distributing a 100 page Ebook “.NET Interview Questions “. If you want this ebook please share this video in your facebook/twitter/linkedin account and email us on questpond@questpond.com with the shared link and we will email you the PDF.

Nguồn:https://wijstaanvooronzegrondrechten.org/

30 COMMENTS

  1. please dont misguide people … its wrong that you can't create the object of a class with private constructor … yes you can !!! by making a parametrized public constructor and then you will be able to create an object of that class by this parametrized constructor …. but you have repeatedly said you can't make objects, this may result in loss of job for a candidate so be careful

  2. Hi Sir Please let me know when should I use private constructor and when should I use static class. Because both have mostly common scenarios.?

  3. Classes with private constructors are used for utility classes but I read on Microsoft documentation that static classes should also be used for the same purpose. Which is better out of these two.

  4. wrong !!!! an instance of a class with a private constructor can be created (how ? just add another parametrrrized constructor to the class)

  5. Classes with private constructors can be used within a Factory design pattern, i.e. a static method within this class can take various parameters and return the instance of this class.

  6. When you say there is one instance of the object I think it is incorrect because there are no instances of class1. The reason you could call getData () was because you made it static. Also to get a single instance ( ie implement singleton ) we would still need to write class1 obj = new class1() in class1 itself.

  7. Private constructor Statement given in above video is partially true because we can create an object of a class if the class has private constructor and at least have one public constructor with it.

    The use of private constructor is for implementing singleton class.

  8. I get the point of using a private constructor. In my view Static class with static methods would have achieved the same result right in the sense for the purposes of Utility, and other similar type of classes we would want only one instance of class. Can you please explain the differences between using class Util with private constructor, static method A  Vs static  class Utils{ static void A()…  ?

  9. The correct statement should be "A class with ONLY private constructor can not be inherited or instantiated."  A class with private constructor can be inherited or instantiated if there is at least one public constructor.  Using private constructor to create static or sealed/singleton classes is misconception.

    The actual use of private constructor which is to be called from public constructors to perform some common initialization as mentioned in the Base Constructor section at http://stackoverflow.com/questions/2585836/why-do-we-need-a-private-constructor.

    The following is an example –

    public class MyClass
    {
         private MyClass() {} // Private constructor
         public MyClass(string inStr) : this() {}
    }

    public class MyClass2 : MyClass // Inherited from MyClass
    {
        public MyClass2(string inStr) : base(inStr) {}
    }

  10. in fact a class can have a private constructor (with a parameter) and a public constructor without a parameter and the class would allow construction normally (new), a private constructor maybe used by some logic which is completely internal to the class, for example you have a function inside the class which creates an instance of the class passing some parameter

  11. you can include static method into class1 called CreateObject and return type is class1 , and create object like this class1 class = class1.CreateObject();

  12. Thanks for the video.
    Regarding not be able to inherit from a class with a private constructor, I actually found that it is possible to inherit if you define a parametrized public constructor in the base and derived, both with same signature. Unfortunately this misconception is very common among developers.

  13. @gerardssw The question we posted was what is the use of private constructor and not static classes. I do not like static classes to stop object creation as i can not inherit further. I do not want to loose that OOP feature.

    if i want only one instance i would use Static classes.

  14. @jockymc I was thinking the same thing and there is a good post on kodefuguru on the "use of a private constructor" by Chris Eargle, I'm sorry it won't let me post the link

LEAVE A REPLY

Please enter your comment!
Please enter your name here