Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果它的日期时间类型相同,为什么会抛出异常_C# - Fatal编程技术网

C# 如果它的日期时间类型相同,为什么会抛出异常

C# 如果它的日期时间类型相同,为什么会抛出异常,c#,C#,我只是想理解为什么会抛出这个异常 无法将类型“System.DateTime”隐式转换为“System.DateTime”。存在显式转换(是否缺少强制转换?) 这就是我想做的 DateTime testconverttime = story.BroadcastOn;`//this is where it throws exception story.BroadcastOn是我从数据库中获取的日期时间值(例如:23/03/2012 1:56 Pm) 我试图将时间从12小时转换为24小时格式

我只是想理解为什么会抛出这个异常

无法将类型“System.DateTime”隐式转换为“System.DateTime”。存在显式转换(是否缺少强制转换?)

这就是我想做的

   DateTime testconverttime = story.BroadcastOn;`//this is where it throws exception
story.BroadcastOn是我从数据库中获取的日期时间值(例如:23/03/2012 1:56 Pm)

我试图将时间从12小时转换为24小时格式,这就是我试图做的

   DateTime testconverttime = story.BroadcastOn;`//this is where it throws exception
所以我必须使用解析来解决这个问题,就像下面这样,但这对我来说没有意义

     if (!string.IsNullOrEmpty(story.BroadcastOn.ToString()))
            {
                 DateTime localTime = story.BroadcastOn.Value;//Thanks all for the great suggestion.
                converttime = localTime.ToString("dd/MMM/yyyy HH:mm ", CultureInfo.CurrentCulture);
            }

我已经将我的12小时转换为24小时,但为了理解异常情况,有人会给我一个解释。

试试这个,假设可空类型有一个值:

DateTime testconverttime = story.BroadcastOn.Value;
它是一个可为null的类型(也可以具有null状态)

可空值类型(DateTime是值类型)具有空值(无值)的概念。所以,例如,如果数据库中的datetime列具有空值,那么您可以使用
Nullable
或在短datetime中?以存储来自该列的值


关于
DateTime.ToString()
String.ToDateTime()
:这叫做
yo
编程。您可能在Debugger中看到有一个有效的
DateTime
,它是通过调用
ToString()
给出的,但是,在将来,不要试图通过这种
yo-yo技术将一个类型强制转换为另一个类型。如果确定story.BroadcastOn.Value是否为空,则需要使用它。

尝试将其强制转换为
日期时间,如下所示:

DateTime testconverttime = (DateTime)story.BroadcastOn

DateTime?
DateTime
不是同一类型<代码>日期时间?
DateTime
类型的类型

您应该检查它是否为null,然后强制转换或检索可为null的值:

if (story.BroadcastOn.HasValue) {
    var broadcastOn = story.BroadcastOn.Value;
    // do stuff with broadcastOn
} else {
    // handle null BroadcastOn
}


使用
.HasValue
或与
null
进行比较,以及使用
.Value
并强制转换为不可为null的类型应该是等效的。您也可以使用
??
运算符

DateTime?
DateTime
不是同一类型,因此不能将
DateTime?
隐式分配给
DateTime

您需要显式强制转换它,或者通过
DateTime
value
属性分配值。但是,如果
BroadcastOn
为null,则两个方法都会引发异常

如果您不知道
broadcasting on
不是null,那么最好使用null合并运算符:

DateTime dt = story.BroadcastOn ?? default(DateTime); 

谢谢ongle,在实践中,我试着像story.BroadcastOn.ToString(dd/MMM/yyyy HH:mm),CultureInfo.CurrentCulture那样做,这个假设是转换,而不是将日期时间再次转换为日期时间,然后将字符串转换为所有这一行代码谢谢大家的精彩解释,我从来不知道这是两种不同的类型,也有空状态。非常感谢大家启发meam在我的代码中做类似的事情,比如if(!string.IsNullOrEmpty)(story.BroadcastOn.ToString()){DateTime localTime=DateTime.Parse(story.BroadcastOn.ToString());converttime=localTime.ToString(“dd/MMM/yyyy HH:mm”,CultureInfo.CurrentCulture);},这是一个很好的解释,非常感谢
DateTime dt = story.BroadcastOn ?? default(DateTime);