C# 如何将 DateTime.TryParse 与 Nullable<DateTime> 一起使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/192121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I use DateTime.TryParse with a Nullable<DateTime>?
提问by Brian Sullivan
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.TryParse 方法将字符串的日期时间值转换为 Nullable。但是当我尝试这个时:
DateTime? d;
bool success = DateTime.TryParse("some date text", out (DateTime)d);
the compiler tells me
编译器告诉我
'out' argument is not classified as a variable
'out' 参数不被归类为变量
Not sure what I need to do here. I've also tried:
不确定我需要在这里做什么。我也试过:
out (DateTime)d.Value
and that doesn't work either. Any ideas?
这也不起作用。有任何想法吗?
采纳答案by Jason Kealey
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?)
(可能有更优雅的解决方案,但你为什么不简单地做一些上面的事情呢?)
回答by Jon Skeet
As Jason says, you can create a variable of the right type and pass that. You might want to encapsulate it in your own method:
正如杰森所说,你可以创建一个正确类型的变量并传递它。您可能希望将其封装在您自己的方法中:
public static DateTime? TryParse(string text)
{
DateTime date;
if (DateTime.TryParse(text, out date))
{
return date;
}
else
{
return null;
}
}
... or if you like the conditional operator:
...或者如果您喜欢条件运算符:
public static DateTime? TryParse(string text)
{
DateTime date;
return DateTime.TryParse(text, out date) ? date : (DateTime?) null;
}
Or in C# 7:
或者在 C# 7 中:
public static DateTime? TryParse(string text) =>
DateTime.TryParse(text, out var date) ? date : (DateTime?) null;
回答by Binary Worrier
You can't because Nullable<DateTime>
is a different type to DateTime
.
You need to write your own function to do it,
你不能因为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;
}
Hope this helps :)
希望这可以帮助 :)
EDIT:Removed the (obviously) improperly tested extension method, because (as Pointed out by some bad hoor) extension methods that attempt to change the "this" parameter will not work with Value Types.
编辑:删除了(显然)未正确测试的扩展方法,因为(正如一些糟糕的人指出的那样)尝试更改“this”参数的扩展方法不适用于值类型。
P.S. The Bad Hoor in question is an old friend :)
PS The Bad Hoor 是一位老朋友 :)
回答by Binary Worrier
Here is a slightly concised edition of what Jason suggested:
以下是杰森建议的稍微简明的版本:
DateTime? d; DateTime dt;
d = DateTime.TryParse(DateTime.Now.ToString(), out dt)? dt : (DateTime?)null;
回答by JStrahl
I don't see why Microsoft didn't handle this. A smart little utility method to deal with this (I had the issue with int, but replacing int with DateTime will be the same effect, could be.....
我不明白为什么微软没有处理这个。一个聪明的小实用方法来处理这个问题(我有 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;
}
}
回答by user2687864
What about creating an extension method?
创建扩展方法怎么样?
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;
}
}
回答by monsieurgutix
Alternatively, if you are not concerned with the possible exception raised, you could change TryParse for Parse:
或者,如果您不关心可能引发的异常,您可以将 TryParse 更改为 Parse:
DateTime? d = DateTime.Parse("some valid text");
Although there won't be a boolean indicating success either, it could be practical in some situations where you know that the input text will always be valid.
尽管也不会有指示成功的布尔值,但在您知道输入文本始终有效的某些情况下,它可能是实用的。
回答by cpcolella
This is the one liner you're looking fo:
这是您正在寻找的一种衬垫:
DateTime? d = DateTime.TryParse("some date text", out DateTime dt) ? dt : null;
If you want to make it a proper TryParse pseudo-extension method, you can do this:
如果你想让它成为一个合适的 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;
}
}
回答by user1267054
Here's a single line solution:
这是一个单行解决方案:
DateTime? d = DateTime.TryParse("text", out DateTime parseDate) ? parseDate : (DateTime?)null;