126

我想使用 DateTime.TryParse 方法将字符串的日期时间值转换为 Nullable。但是当我尝试这个时:

DateTime? d;
bool success = DateTime.TryParse("some date text", out (DateTime)d);

编译器告诉我

'out' 参数未被归类为变量

不知道我需要在这里做什么。我也试过:

out (DateTime)d.Value 

这也不起作用。有任何想法吗?

4

9 回答 9

175

正如 Jason 所说,您可以创建一个正确类型的变量并传递它。您可能希望将其封装在您自己的方法中:

public static DateTime? TryParse(string text)
{
    DateTime date;
    if (DateTime.TryParse(text, out date))
    {
        return date;
    }
    else
    {
        return null;
    }
}

...或者如果您喜欢条件运算符:

public static DateTime? TryParse(string text)
{
    DateTime date;
    return DateTime.TryParse(text, out date) ? date : (DateTime?) null;
}

或者在 C# 7 中:

public static DateTime? TryParse(string text) =>
    DateTime.TryParse(text, out var date) ? date : (DateTime?) null;
于 2008-10-10T16:35:32.077 回答
133
DateTime? d=null;
DateTime d2;
bool success = DateTime.TryParse("some date text", out d2);
if (success) d=d2;

(可能有更优雅的解决方案,但你为什么不简单地做上面的事情呢?)

于 2008-10-10T16:27:38.897 回答
22

这是 Jason 建议的稍微简洁的版本:

DateTime? d; DateTime dt;
d = DateTime.TryParse(DateTime.Now.ToString(), out dt)? dt : (DateTime?)null;
于 2008-10-31T21:41:14.310 回答
18

您不能因为Nullable<DateTime>DateTime. 您需要编写自己的函数来执行此操作,

public bool TryParse(string text, out Nullable<DateTime> nDate)
{
    DateTime date;
    bool isParsed = DateTime.TryParse(text, out date);
    if (isParsed)
        nDate = new Nullable<DateTime>(date);
    else
        nDate = new Nullable<DateTime>();
    return isParsed;
}

希望这可以帮助 :)

编辑: 删除(显然)未正确测试的扩展方法,因为(正如一些坏话所指出的那样)尝试更改“this”参数的扩展方法不适用于值类型。

PS 有问题的 Bad Hoor 是一位老朋友 :)

于 2008-10-10T16:46:25.223 回答
4

创建扩展方法怎么样?

public static class NullableExtensions
{
    public static bool TryParse(this DateTime? dateTime, string dateString, out DateTime? result)
    {
        DateTime tempDate;
        if(! DateTime.TryParse(dateString,out tempDate))
        {
            result = null;
            return false;
        }

        result = tempDate;
        return true;

    }
}
于 2012-06-24T22:06:36.690 回答
2

这是您正在寻找的一个班轮:

DateTime? d = DateTime.TryParse("some date text", out DateTime dt) ? dt : null;

如果你想让它成为一个合适的 TryParse 伪扩展方法,你可以这样做:

public static bool TryParse(string text, out DateTime? dt)
{
    if (DateTime.TryParse(text, out DateTime date))
    {
        dt = date;
        return true;
    }
    else
    {
        dt = null;
        return false;
    }
}
于 2019-10-18T18:41:19.567 回答
1

我不明白为什么微软没有处理这个问题。一个聪明的小实用方法来处理这个问题(我遇到了 int 的问题,但是用 DateTime 替换 int 将是相同的效果,可能是.....

    public static bool NullableValueTryParse(string text, out int? nInt)
    {
        int value;
        if (int.TryParse(text, out value))
        {
            nInt = value;
            return true;
        }
        else
        {
            nInt = null;
            return false;
        }
    }
于 2009-10-12T18:20:09.057 回答
1

这是一个单行解决方案:

DateTime? d = DateTime.TryParse("text", out DateTime parseDate) ? parseDate : (DateTime?)null;
于 2020-02-18T21:12:42.627 回答
-3

或者,如果您不关心可能引发的异常,您可以将 TryParse 更改为 Parse:

DateTime? d = DateTime.Parse("some valid text");

尽管也不会有布尔值指示成功,但在您知道输入文本将始终有效的某些情况下,它可能是实用的。

于 2018-08-07T19:45:49.107 回答