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?
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
No Constructors implicitly return class type same as java also.
Constructors have no return type and do not return anything, it's not a method. From C# language specification 4.0:
An instance constructor is a member that implements the actions required to initialize an instance of a class. Instance constructors are declared using constructor-declarations.
attributes opt constructor-modifiers opt constructor-declarator constructor-body
A constructor is declared like a method with no return type and the same name as the containing class
There is no return type here.
Constructor is invoked through the new
operator, which is the one, that returns an instance of an object.
来源:https://stackoverflow.com/questions/14949621/are-class-constructors-void-by-default