In C# 3.0, I’m doing the code below to declare a DateTime property and a int age property that is read only.
public class MyClass{
public DateTime dateOfBirth{ get; set; }
public int age { get; }
public MyClass(){}
public int CalculateAge(){}
}
But how can I get this updated age (that it’s read only) when someone enters his date of birth in a form, for example?
Don’t store the age, instead calculate it when get of the age property is executed:
public int Age { get { return 100; } }But instead of returning 100, you do the calculation.