如何使用DateTime.TryParse一个可空<&日期时间GT ;? [英] How do I use DateTime.TryParse with a Nullable<DateTime>?

查看:314
本文介绍了如何使用DateTime.TryParse一个可空<&日期时间GT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用DateTime.TryParse方法来获取一个字符串转换为可为空值的日期时间值。但是,当我试试这个:

 日期时间? D组;
BOOL成功= DateTime.TryParse(一些日期文本,出(日期时间)D);

编译器告诉我


  

出的说法是不属于变量


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

 出(DATETIME)d.Value

和不工作的。任何想法?


解决方案

 日期时间? D = NULL;
日期时间D2;
BOOL成功= DateTime.TryParse(一些日期文本,出D2);
如果(成功)D = D2;

(可能有更优雅的解决方案,但你为什么不只是做一些如上面?)

I want to use the DateTime.TryParse method to get the datetime value of a string into a Nullable. But when I try this:

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

the compiler tells me

'out' argument is not classified as a variable

Not sure what I need to do here. I've also tried:

out (DateTime)d.Value 

and that doesn't work either. Any ideas?

解决方案

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

(There might be more elegant solutions, but why don't you simply do something as above?)

这篇关于如何使用DateTime.TryParse一个可空<&日期时间GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆