C# Programming - Constructors - Discussion

Discussion Forum : Constructors - General Questions (Q.No. 14)
14.
Is it possible for you to prevent an object from being created by using zero argument constructor?
Yes
No
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
7 comments Page 1 of 1.

Marc said:   1 decade ago
Even with a private constructor, it is possible to instantiate the object internally (from within a nested class for example.) So, the answer is technically no, you cannot COMPLETELY prevent the object from being instantiated (the way to do that would be to use a class that is defined as static.).
(1)

Sundar said:   1 decade ago
Yes. By using private constructor.

A private constructor is a special instance constructor. It is commonly used in classes that contain static members only. If a class has one or more private constructors and no public constructors, then other classes (except nested classes) are not allowed to create instances of this class. For example:

class NLog
{
// Private Constructor:
private NLog() {}

public static double e = 2.71828;
}

The declaration of the empty constructor prevents the automatic generation of a default constructor. Note that if you don't use an access modifier with the constructor it will still be private by default. However, the private modifier is usually used explicitly to make it clear that the class cannot be instantiated.

Private constructors are useful to prevent creation of a class when there are no instance fields or methods, such as the Math class, or when a method is called to obtain an instance of a class.

Example

The following is an example of a class using a private constructor.

// PrivateCtor1.cs
using System;

public class MyClass
{
private MyClass() {}
public static int counter;
public static int IncrementCounter()
{
return ++counter;
}
}

class MainClass
{
static void Main()
{
// If you uncomment the following statement, it will generate
// an error because the constructor is inaccessible:
// MyClass myObject = new MyClass(); // Error
MyClass.counter = 100;
MyClass.IncrementCounter();
Console.WriteLine("New count: {0}", MyClass.counter);
}
}

Output
New count: 101

Notice that if you uncomment the following statement from the example, it will generate an error because the constructor is inaccessible due to its protection level:

// MyClass myObject = new MyClass(); // error

Hope this will help you. Have a nice day!

Sadiq said:   1 decade ago
When a construstor is not explicitly written for a class a default constructor is implicitly created by the CLR with zero arguments and sets all the data members with default values. Inorder to prevent this user can write his own constructor with aruguments. By creating the constructor with arguments user can not create an object for this class with zero aruguments he/she has to specify the arguments while creating the object for this class.

Nikita said:   1 decade ago
Hi,

Answer is very simple yes we can prevent an object by zero argument by making parameterized constructor.

Kavitha said:   1 decade ago
Can anyone explain me the question clearly?

Akshay said:   10 years ago
Hi,

If the Instance of the class is Created without any parameters then, Default Constructor of the class is executed.

Prince Gajjar said:   2 years ago
By using a Private Constructor, we can prevent an object from being created by using the zero-argument constructor.

Post your comments here:

Your comments will be displayed after verification.