计算c#中的年龄还不错

时间:2013-06-06 10:32:27

标签: c# .net

我有财产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);
}

在编译时我遇到错误

The best overloaded method match for .... has some invalid arguments
and
cannot convert from 'System.DateTime?' to System.DateTime

9 个答案:

答案 0 :(得分:3)

  

....的最佳重载方法匹配有一些无效的参数,无法从'System.DateTime?'转换到System.DateTime

那么你试图解决这个问题呢?错误很明显:您将System.DateTime?参数传递给接受System.DateTime的函数。

要修复它,请更改方法签名

CalculateAge(DateTime? birthDate, DateTime now)
{
    if (!birthDate.HasValue)
    {
        return -1; // ?
    }
}

但是如你所见,这是无用的。所以改变电话:

if (Dob.HasValue)
{
    Age = CalculateAge(Dob.Value, DateTime.Now);
}

最终你只想使用一个属性:

public virtual int Age { 
    get
    {
        if (!Dob.HasValue)
        {
            throw new Exception(); // ?
            return -1; // ?
        }

        return CalculateAge(Dob.Value);
    }
}

如你所知,无论你在哪里解决这个问题:你只需要检查某处是否可以为空(?)出生日期包含一个值。

答案 1 :(得分:2)

您应该传递DateTime而不是可以为空的DateTime

Age = CalculateAge((Dob.HasValue ? Dob.Value : DateTime.Now), DateTime.Now);

或更改接收方式

private int CalculateAge(DateTime? birthDate, DateTime now)

并应用所有必要的检查以避免NullReferenceExceptions

答案 2 :(得分:1)

CalculateAge方法接受DateTime参数,并且您传递DateTime?(可为空DateTime)。您必须更改其中一个,或者转换为DateTime

此外,第二个参数没有真正的原因,因为DateTime.Now可以在方法内部计算。

第三,关于计算年龄的问题,请参阅类似的问题:Calculate age in C#

答案 3 :(得分:1)

查看方法声明

private int CalculateAge(DateTime birthDate, DateTime now)

和DateOfBirth声明

public virtual DateTime? Dob { get; set; }

您不能将可空的DateTime属性用作第一个参数。将声明更改为

private int CalculateAge(DateTime? birthDate, DateTime now)

或从Dob属性中删除可空性

public virtual DateTime Dob { get; set; }

答案 4 :(得分:0)

您可以使用

public static int GetAge(DateTime birthDate)
{
DateTime n = DateTime.Now; // To avoid a race condition around midnight
int age = n.Year - birthDate.Year;

if (n.Month < birthDate.Month || (n.Month == birthDate.Month && n.Day < birthDate.Day))
age--;

return age;
}

答案 5 :(得分:0)

use private int CalculateAge(DateTime? birthDate, DateTime now)

而不是

private int CalculateAge(DateTime birthDate, DateTime now)

答案 6 :(得分:0)

使用TimeSpan来获取这里提到的两个日期之间的差异:

private int CalculateAge(DateTime birthDate, DateTime now)
{
   TimeSpan span = now.Subtract(birthDate);     
   return (int)span.TotalDays / 365;  
}

答案 7 :(得分:0)

更改方法定义并检查birthDate是否具有值(不为空)

private int CalculateAge(DateTime? birthDate, DateTime now)
{
   if(birthDate.HasValue)
   {
      int age = now.Year - birthDate.Year;
      if (now.Month < birthDate.Month || (now.Month == birthDate.Month && now.Day < birthDate.Day))
      {
         age--;
      }            
      return age;
   }
   else 
      return 0;
}

答案 8 :(得分:0)

您必须投射日期时间吗?像DateTime一样

(DateTime)Dob

但是,如果您没有在代码中的任何地方处理空日期的可能性,那么为什么要首先让Dob成为可空?