Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 757
aspiring Avatar asked May 06 '26 08:05

aspiring


2 Answers

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

like image 90
Keith Nicholas Avatar answered May 09 '26 01:05

Keith Nicholas


No Constructors implicitly return class type same as java also.

like image 23
Karthi Avatar answered May 09 '26 01:05

Karthi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!