非字符串返回类型的日期检查-设计

本文关键字:检查 设计 日期 字符串 返回类型 | 更新日期: 2023-09-27 17:57:52

public static bool CheckExpired()
{
    DateTime expiryDate, currentDate = DateTime.Today;
    DateTime.TryParse(date, out expiryDate);
    if (expiryDate > currentDate)
    {
        return true;
    }
    else { return false; }
}

这就是我现在拥有的。然而,如果日期格式不正确,我希望有第三种选择。和现在一样,它直接跳到另一个并返回false。

问题是我想要三个可能的结果:

true-->尚未过期

false-->已过期

输入的第三个-->无效日期

我只是纠结于如何到达那里。我知道我可以很容易地使用String返回类型,但有什么办法吗?

非字符串返回类型的日期检查-设计

几个选项;

  1. 使用bool?类型;则可以作为结果返回CCD_ 2。问题是,"null"在你的上下文中没有真正的意义,所以用法不清楚

  2. 如果格式不正确,则抛出Exception。可能会使用法更清晰(不能传递格式错误的日期(,但这意味着你需要在所有调用方中尝试捕获

  3. 使用enum作为返回值,它可以显式地为结果设置名称。

我认为枚举可能在这里最有意义,对该方法的消费者来说也是最清晰的;

public enum ExpiredResult
{
    Expired,
    NotExpired,
    FormatError,
}

您可以使用NullableBool

    public static bool? CheckExpired(string date)
    {
        DateTime expiryDate, currentDate = DateTime.Today;
        if (DateTime.TryParse(date, out expiryDate))
        {
            return expiryDate > currentDate;
        }
        return null;
    }

true-->尚未过期

false-->已过期

null-->输入的无效日期

由于DateTime.TryParse((返回一个bool,如果成功,您可以触发它。

if(DateTime.TryParse(date, out expiryDate))
{
    if (expiryDate > currentDate)
    {
        return true;
    }
    else { return false; }
}
else
{
    Console.Write("Invalid date.");
}

然后使用Nullable而不是bool作为返回类型。

我稍微重构了代码。但这可能是一个解决方案:

public static bool CheckExpired()
{
    DateTime expiryDate, currentDate = DateTime.Today;
    if (!DateTime.TryParse(date, out expiryDate))
    {
         throw new Exception("Invalid date");
    }
    return (expiryDate > currentDate);
}

您可以使用可为null的bool(bool?(或int

如果字符串未能转换为日期时间,它将返回null

public static bool? CheckExpired()
    {
        DateTime expiryDate, currentDate = DateTime.Today;
        DateTime.TryParse(date, out expiryDate);
        if (expiryDate == new DateTime())
        {
            return null;
        }
        if (expiryDate > currentDate)
        {
            return true;
        }
        else 
        { return false; }
    }

听起来像是枚举的作业。。。

我会使用枚举。大致如下:

public static DateValidationResult CheckExpired(string date)
{
    DateTime expiryDate, currentDate = DateTime.Today;
    if (!DateTime.TryParse(date, out expiryDate))
        return DateValidationResult.InvalidDate;
    return expiryDate > currentDate ? DateValidationResult.Ok : DateValidationResult.Fail;
}
public enum DateValidationResult
{
    InvalidDate,
    Ok,
    Fail
}

试试这个:

public static bool? CheckExpired(string date)
{
    DateTime expiryDate;
    DateTime currentDate = DateTime.Today;
    if (!DateTime.TryParse(date, out expiryDate))
    {
        return null;
    }
    return (expiryDate > currentDate);
}

检查null返回值或有效的true/false值,如

string date = "2/4/2013";
bool? isExpired = CheckExpired(date);
if (!isExpired.HasValue)
{
    Console.Write("Invalid date");
}
else if (isExpired.Value)
{
    Console.Write("Expired");
}
else // if (!isExpired.Value)
{
    Console.Write("Valid");
}

就我个人而言,我只想写一个Enum来封装返回状态,类似于以下内容:

public enum ExipiryStatus
{
    Expired,
    NotExpired,
    InvalidDate
}
public static ExipiryStatus CheckExpired(string date)
{
    DateTime expiryDate, currentDate = DateTime.Today;
    if (DateTime.TryParse(date, out expiryDate))
    {
        if (expiryDate > currentDate)
        {
            return ExipiryStatus.Expired;
        }
        else
        {
            return ExipiryStatus.NotExpired;
        }
    }
    else
    {
        return ExipiryStatus.InvalidDate;
    }
}

我真的不喜欢这种东西的可为null的bools。在调用站点上,返回值应该是什么从来都不太清楚。对于枚举,它是显式的。

[编辑]击败它。:(