Search code examples
c#voidclass-constructors

Are class constructors void by default?


I have been reading on class constructors in C#. Examples are showing overloaded class contructors. And all of them do not have a void keyword and neither they have a return value..

e.g.

public Class myClass
{
  public myClass()
  {

  }

  public myClass(int id)
  {

  }
//other class memeber go here...
}

1) So is it correct to say C# constructors are void by default?

2) Does the same applies to Java as well?


Solution

  • no, they are constructors, if anything, you can think of them as returning an object of the class they are from. But, they aren't normal method/functions