Modify the constructor by adding one extra parameter dateOfBirth
. Use this parameter to initialize your property DateOfBirth
. This way, the Person
can never be instantiated without a valid value for DateOfBirth
.
public Person(string firstName, string lastName, DateTime dateOfBirth)
{
FirstName = firstName;
LastName = lastName;
DateOfBirth = dateOfBirth;
}
now you can construct it like this:
Person p = new Person("John", "Smith", new DateTime(2010, 01, 13));
2
solved Need help and a concise explanation for updating an existing constructor in C#