代码之家  ›  专栏  ›  技术社区  ›  Brian Sullivan

如何使用DateTime。使用可为null的<DateTime>进行TryParse?

  •  137
  • Brian Sullivan  · 技术社区  · 16 年前

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

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

    编译器告诉我

    “out”参数未被归类为变量

    我不确定我在这里需要做什么。我也试过:

    out (DateTime)d.Value 
    

    这也行不通。有什么想法吗?

    9 回复  |  直到 9 年前
        1
  •  175
  •   Jon Skeet    8 年前

    正如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;
    
        2
  •  133
  •   Drew Noakes    15 年前
    DateTime? d=null;
    DateTime d2;
    bool success = DateTime.TryParse("some date text", out d2);
    if (success) d=d2;
    

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

        3
  •  22
  •   Sonny Sonny    16 年前

    以下是Jason建议的一个稍微简洁的版本:

    DateTime? d; DateTime dt;
    d = DateTime.TryParse(DateTime.Now.ToString(), out dt)? dt : (DateTime?)null;
    
        4
  •  18
  •   Binary Worrier    16 年前

    你不能因为 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;
    }
    

    希望这能有所帮助:)

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

    附言:这位坏胡尔是我的老朋友:)

        5
  •  4
  •   user2687864    13 年前

    这就是你要找的一条线:

    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;
        }
    }
    
        6
  •  1
  •   JStrahl    15 年前

    创建一个扩展方法怎么样?

    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;
    
        }
    }
    
        7
  •  1
  •   user1267054    5 年前

    我不明白微软为什么不处理这个问题。一个聪明的小实用方法来处理这个问题(我对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;
            }
        }
    
        8
  •  1
  •   cpcolella    4 年前

    以下是一个单线解决方案:

    DateTime? d = DateTime.TryParse("text", out DateTime parseDate) ? parseDate : (DateTime?)null;
    
        9
  •  -3
  •   monsieurgutix    6 年前

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

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

    虽然也没有布尔值表示成功,但在某些情况下,当你知道输入文本始终有效时,它可能是实用的。