I wonder how I could write this type of constructors:
Person p = Person.CreateWithName("pedro");
Person p1 = Person.CreateEmpty();
And having the code of each constructor in separate.
Those are so called factory methods and technically are static methods on the Class (person) that are then called on the class (Person.Create).
Technically they internally create the Person with new - but it can happen with a PRIVATE CONSTRUCTOR.
You just create a static method inside that class, i.e.
class Person {
public Person(string name) {
//Constructor logic
}
public static Person CreatePerson() {
return new Person(string.Empty);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With