Actually, you should not have an
Age
DateTime property. It should be named
DateOfBirth
(for example). The
Age
property can then be computed when you need it, from the date of birth.
This way:
public DateTime DateOfBirth { get; set; }
public int Age
{
get { return (DateTime.Today - DateOfBirth).Days / 365; }
}
Analyze:
-
(DateTime.Today - DateOfBirth)
returns a
TimeSpan
value.
- The total number of days in the TimeSpan is divided by the average number of days in a year to give an approximation of the age.
Note that the Age property is read-only, as it is computed everytime the property getter is called. It thus always returns the current age.
Hope this helps. Kindly.