私はプロパティDateOfBirth
とプロパティを持っていますAge
。
DateOfBirth
はDateTime
データ型であり、Age
データint
型です。
コンストラクター内で人の年齢を計算したいのですが、次のようにしています。
private int CalculateAge(DateTime birthDate, DateTime now)
{
int age = now.Year - birthDate.Year;
if (now.Month < birthDate.Month || (now.Month == birthDate.Month && now.Day < birthDate.Day))
{
age--;
}
return age;
}
public virtual DateTime? Dob { get; set; }
public virtual int Age { get; set; }
public MyObject()
{
Age = CalculateAge(Dob, DateTime.Now);
}
コンパイル時に次のエラーが発生します。
... に最適なオーバーロードされたメソッドの一致には、無効な引数があります
と
「System.DateTime?」から変換できませんか? System.DateTime に