解析和转换之间的区别 [英] Difference between parse and convert
问题描述
我很困惑,我们为什么要使用datetime.parse()而不是convert.toInt32()
请举例给我一个答案。
I am confusing, Why should we use datetime.parse() instead of convert.toInt32()
Please give me an answer with example.
推荐答案
我想说,这两种方法并没有太多共同之处。
I would say, this two methods have not much in common.
public static DateTime Parse(
string s
)
将日期和时间的字符串表示形式转换为其DateTime等效值。
返回值
类型: System.DateTime
一个对象,相当于s中包含的日期和时间。
Converts the string representation of a date and time to its DateTime equivalent.
Return Value
Type: System.DateTime
An object that is equivalent to the date and time contained in s.
public static int ToInt32(
string value
)
将数字的指定字符串表示形式转换为等效的32位有符号整数。
返回值
类型: System.Int32
32位有符号整数,相当于v中的数字如果值为null,则为0或0(零)。
换句话说,您使用第一种方法转换字符串,如01.May 2013 17:45 :25到DateTime,第二个将字符串转换为-25到整数!
Converts the specified string representation of a number to an equivalent 32-bit signed integer.
Return Value
Type: System.Int32
A 32-bit signed integer that is equivalent to the number in value, or 0 (zero) if value is null.
In other words, you use the first method to convert a string like "01.May 2013 17:45:25" to DateTime and the second one to convert a string like "-25" to an integer!
du [DE]是对的,但如果你的意思是<$ c之间的区别$ c> int.Parse 和Convert.ToInt32
,然后看看这里:
http://stackoverflow.com/questions/199470/whats-the-main -difference-between-parse-and-convert-toint32 [ ^ ]
如果你的意思是DateTime.Parse之间的差异
和Convert.ToDateTime
,然后看看这里:
http://stackoverflow.com/questions/8192520/any-difference-between-parse-and-convert [ ^ ]
http://forums.asp.net/t/1710269.aspx/1 [ ^ ]
http://www.pcreview.co.uk/forums/convert-todatetime-vs-system-datetime-parse- t2509851.html [ ^ ]
du[DE] is right, but if you mean the difference betweenint.Parse
andConvert.ToInt32
, then have a look here:
http://stackoverflow.com/questions/199470/whats-the-main-difference-between-int-parse-and-convert-toint32[^]
And if you mean the difference betweenDateTime.Parse
andConvert.ToDateTime
, then have a look here:
http://stackoverflow.com/questions/8192520/any-difference-between-parse-and-convert[^]
http://forums.asp.net/t/1710269.aspx/1[^]
http://www.pcreview.co.uk/forums/convert-todatetime-vs-system-datetime-parse-t2509851.html[^]
当您拥有日期或时间(或两者)的字符串表示时,您应该使用Parse方法,并且需要将其转换为DateTime对象(参见DateTime.Parse方法 [ ^ ]。
另一方面,DatetTime.IConvertible.ToInt32 [ ^ ]将朝相反的方向发展(它实际上会抛出,如文档所述)。
You should use the Parse method when you have the string representation of a date or time (or both) and need to convert it into a DateTime object (see DateTime.Parse Method[^].
On the other hand, DatetTime.IConvertible.ToInt32[^] would go in the opposite direction (it would actually throw, as stated by the documentation).
这篇关于解析和转换之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!