C# 将空白字符串解析为DateTime会使应用程序不可关闭
当我在C# 将空白字符串解析为DateTime会使应用程序不可关闭,c#,wpf,datetime,C#,Wpf,Datetime,当我在DateTime.Parse中使用空白字符串作为参数时,关闭所有窗口后,应用程序仍在运行,如下所示: txtBirthDate.SelectedDate = ("" == empBirthDate) ? DateTime.Parse("") : DateTime.Parse(empBirthDate); txtBirthDate.SelectedDate = String.IsNullOrEmpty(empBirthDate) ?
DateTime.Parse
中使用空白字符串作为参数时,关闭所有窗口后,应用程序仍在运行,如下所示:
txtBirthDate.SelectedDate = ("" == empBirthDate) ? DateTime.Parse("") : DateTime.Parse(empBirthDate);
txtBirthDate.SelectedDate = String.IsNullOrEmpty(empBirthDate) ?
DateTime.MinValue :
DateTime.Parse(empBirthDate);
DateTime inDate;
string currentFormat = "MM/dd/yyyy";
DateTime.TryParseExact(empBirthDate, currentFormat , CultureInfo.InvariantCulture, DateTimeStyles.None, out inDate);
txtBirthDate.SelectedDate = inDate;
但当我输入日期时,例如11/26/1995
,应用程序在我关闭所有窗口后停止运行:
txtBirthDate.SelectedDate = ("" == empBirthDate) ? DateTime.Parse("11/26/1995") : DateTime.Parse(empBirthDate);
这是
DateTime.Parse
的一项功能,还是其他功能?如果输入字符串为空或为空,则DateTime.Parse
无法解析空字符串,因此可以返回DateTime.MinValue
或DateTime.Today
。在这种情况下,代码如下所示:
txtBirthDate.SelectedDate = ("" == empBirthDate) ? DateTime.Parse("") : DateTime.Parse(empBirthDate);
txtBirthDate.SelectedDate = String.IsNullOrEmpty(empBirthDate) ?
DateTime.MinValue :
DateTime.Parse(empBirthDate);
DateTime inDate;
string currentFormat = "MM/dd/yyyy";
DateTime.TryParseExact(empBirthDate, currentFormat , CultureInfo.InvariantCulture, DateTimeStyles.None, out inDate);
txtBirthDate.SelectedDate = inDate;
如果您知道变量empbirathdate
中的日期格式,则使用TryParseExact
会变得更容易,在这种情况下,如果转换失败,inDate
变量的值将为DateTime.MinValue
,否则它将具有正确的值。所以你可以这样尝试:
txtBirthDate.SelectedDate = ("" == empBirthDate) ? DateTime.Parse("") : DateTime.Parse(empBirthDate);
txtBirthDate.SelectedDate = String.IsNullOrEmpty(empBirthDate) ?
DateTime.MinValue :
DateTime.Parse(empBirthDate);
DateTime inDate;
string currentFormat = "MM/dd/yyyy";
DateTime.TryParseExact(empBirthDate, currentFormat , CultureInfo.InvariantCulture, DateTimeStyles.None, out inDate);
txtBirthDate.SelectedDate = inDate;
DateTime.Parse
无法解析空字符串,因此,如果输入字符串为null或空,则可以返回DateTime.MinValue
或DateTime.Today
。在这种情况下,代码如下所示:
txtBirthDate.SelectedDate = ("" == empBirthDate) ? DateTime.Parse("") : DateTime.Parse(empBirthDate);
txtBirthDate.SelectedDate = String.IsNullOrEmpty(empBirthDate) ?
DateTime.MinValue :
DateTime.Parse(empBirthDate);
DateTime inDate;
string currentFormat = "MM/dd/yyyy";
DateTime.TryParseExact(empBirthDate, currentFormat , CultureInfo.InvariantCulture, DateTimeStyles.None, out inDate);
txtBirthDate.SelectedDate = inDate;
如果您知道变量empbirathdate
中的日期格式,则使用TryParseExact
会变得更容易,在这种情况下,如果转换失败,inDate
变量的值将为DateTime.MinValue
,否则它将具有正确的值。所以你可以这样尝试:
txtBirthDate.SelectedDate = ("" == empBirthDate) ? DateTime.Parse("") : DateTime.Parse(empBirthDate);
txtBirthDate.SelectedDate = String.IsNullOrEmpty(empBirthDate) ?
DateTime.MinValue :
DateTime.Parse(empBirthDate);
DateTime inDate;
string currentFormat = "MM/dd/yyyy";
DateTime.TryParseExact(empBirthDate, currentFormat , CultureInfo.InvariantCulture, DateTimeStyles.None, out inDate);
txtBirthDate.SelectedDate = inDate;
基本上不走运是对的。但是第一个代码示例在输入无效的情况下仍然会抛出异常。而第二个示例必须按照以下方式进行修改:
DateTime inDate;
string currentFormat = "MM/dd/yyyy";
if (DateTime.TryParseExact(empBirthDate, currentFormat , CultureInfo.InvariantCulture, DateTimeStyles.None, out inDate))
{
txtBirthDate.SelectedDate = inDate;
}
也不使用DATEMEM.MIVALL考虑使用空的DATETIME(定义为“DATETIME?”)。在某些情况下,这更合适。
基本上不走运是正确的。但是第一个代码示例在输入无效的情况下仍然会抛出异常。而第二个示例必须按照以下方式进行修改:DateTime inDate;
string currentFormat = "MM/dd/yyyy";
if (DateTime.TryParseExact(empBirthDate, currentFormat , CultureInfo.InvariantCulture, DateTimeStyles.None, out inDate))
{
txtBirthDate.SelectedDate = inDate;
}
也不使用DATEMEM.MIVALL考虑使用空的DATETIME(定义为“DATETIME?”)。在某些情况下,这会更合适。
首先,DateTime.Parse(“”
抛出一个FormatException
,为什么要故意这样抛出一个异常?我认为稍微了解一下上下文会有很大帮助。另外,默认情况下,不信任输入也是一个好主意。我会研究使用DateTime.TryParse
检查输入-可能会有帮助。@mok我不知道,但在我将代码放入try-catch后确实出现了一个错误,可能这就是为什么关闭所有窗口后应用程序没有停止运行的原因。如果不可能有一个空白字符串,那么当empbirathDate
没有值时,我能做什么呢?首先,DateTime.Parse(“”
抛出一个FormatException
,为什么你故意这样抛出一个异常?我想稍微了解一下上下文会有很大帮助。另外,默认情况下,不信任输入也是一个好主意。我会研究使用DateTime.TryParse
检查输入-可能会有帮助。@mok我不知道,但在我将代码放入try-catch后确实出现了一个错误,可能这就是为什么关闭所有窗口后应用程序没有停止运行的原因。如果不可能有空字符串,那么当empbirathdate
没有值时,我可以做什么选择?这很好。奇怪的是,String.IsNullOrEmpty(empbirathDate)
比好吗?==empbirathDate
?是的,比=
好这很好。奇怪的是,String.IsNullOrEmpty(empbirathDate)
优于“”==empbirathDate
?是的,它优于=
比较