datetime变量出错 [英] Error in datetime variable
问题描述
string carName =;
DateTime dateFrom =;
有错误吗?
string carName = "";
DateTime dateFrom = "";
Is there any error?
推荐答案
是的。下一个!
哦,你想了解更多细节吗?好的......
如果你试图编译它,你会收到一个错误:
Yes. Next!
Oh, you want more details? OK...
if you try to compile that, you will get an error:
Cannot implicitly convert type 'string' to 'System.DateTime'
因为你不能只为一个DateTime分配一个字符串,而不是在一个圆孔中推一个方形钉。
DateTime不是字符串数据类型:它是一个值,表示自任意时间点以来的毫秒数。即使你想给它分配一个空白值,你也不能,因为就像整数
它没有空白数字的概念 - 它有一个零,就像整数做(DateTime.MinValue)但它没有空白值,所以即使你尝试了正确的方式,通过使用DateTime.Parse或DateTime将字符串值赋值给DateTime .ParseExact如果你试图解析一个空字符串会引发异常!
想想你想要实现的目标,并且可能有更好的方法尝试到达那里 - 但这不会起作用!
Because you can't just assign a string to a DateTime any more than you can push a square peg in a round hole.
DateTime is not a string data type: it's a value which represents the number of milliseconds since and arbitrary point of time. Even if you wanted to assign a "blank" value to it, you can't, because just like integer
it doesn't have a concept of "blank number" - it has a zero, just like integers do (DateTime.MinValue) but it doesn't have a "blank" value, so even if your tried the "proper" way to assign a string value to a DateTime by using DateTime.Parse or DateTime.ParseExact it would throw an exception if you tried to parse a blank string!
Think about what you are trying to achieve and there may be a better way to try and get there - but that isn't going to work!
我希望你非常敏锐地观察下面的内容:
I want you to observe the below stuff very keenly:
string carName =;
DateTime dateFrom =;
string carName = "";
DateTime dateFrom = "";
以下代码特别之处???为什么编译???
Whats special in the below code??? Why does it compile???
string carName = ;
DateTime dateFrom = DateTime.Parse();
string carName = "";
DateTime dateFrom = DateTime.Parse("");
如果我这样做?
What if i do?
DateTime dateFrom = Convert.ToDateTime();
DateTime dateFrom = Convert.ToDateTime("");
最后一期:
什么如果你问我50
Last question:
What if you ask me 50
我给你50Rs有什么区别?
谢谢
and i give you 50Rs what is the difference?
Thanks
这篇关于datetime变量出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!