What will happen if you don't specify any constructor in your class and try to call the constructor in the client code?
The answer is simple, that will work, C# will automatically call the invisible default constructor without parameters, (Unless you don't specify any private constructor in the class), the following code will work.
The answer is simple, that will work, C# will automatically call the invisible default constructor without parameters, (Unless you don't specify any private constructor in the class), the following code will work.
class ClassA
{
public void DoAction(int Count)
{
Console.WriteLine("Count: " + Count.ToString());
}
}
class Program
{
static void Main(string[] args)
{
ClassA ClsAObj1 = new
ClassA();
}
}
The following code will not work:
class ClassA
{
private ClassA()
{
}
public void DoAction(int Count)
{
Console.WriteLine("Count: " + Count.ToString());
}
}
class
Program
{
static void Main(string[] args)
{
ClassA ClsAObj1 = new
ClassA();
}
}
Error Message:
'ClassA.ClassA()' is inaccessible due to its protection level.
Comments