Yes, C# class without constructor is possible.
We can have a class without any constructor in C#. If we don’t declare any constructors in a class, the compiler automatically provides a public parameterless constructor. And it is ok to instantiate the class without a constructor.
In the below C# code example program, we don’t have any constructor in the class Person.
class Person
{
//This class has no constructor
public void getPersonProfile()
{
Console.WriteLine("Base class:getPersonProfile() have no constructor");
}
}
class Program
{
static void Main(string[] args)
{
//Create object of person
Person person = new Person();
person.getPersonProfile();
}
}
No comments:
Post a Comment