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

preview_player
Показать описание

See our other Step by Step video series below :-

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.

Рекомендации по теме
Комментарии
Автор

Do not miss our Interview Question video series

dnfvideo
Автор

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.

amitpatil-pkpx
Автор

Every one have knowledge about technology but sir you have very2 deeply knowledge of c# or whatever you teach us

technicalindian
Автор

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

sevenshs
Автор

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.

SuchismitaGhosh
Автор

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()...  ?

LotusShiv
Автор

@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.

dnfvideo
Автор

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.?

sachinsachan
Автор

Does this have the same usage of a static class?

itjdsbk
Автор

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.

mouradbarakat
Автор

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.

ankushjain
Автор

Yes it can and it is used in the singleton pattern.

MichaelSaull
Автор

This video was uploaded 11 years ago!!! Wow!!

chintalapativenkataramarahul
Автор

So whats the difference between doing this and declaring a static class?

damonp
Автор

then i would have created static classes for directly calling methods .
still why i need private constructor for a class??

himanshuupreti
Автор

Great video, but cant i just have a class with static methods?

diogosilva
Автор

Instead of creating the private constructor we can create it as Static will also provide us same functionality

infoboopathi
Автор

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

ARTSOFT
Автор

but why we dont use static class then directly for utilities and helper funtions??

prateekbhardwaj
Автор

But with a private constructor, can we add parameterized public constructor ?

Shakti_Kr_Singh