What is the difference between Convert and Parse?

前端 未结 4 866
广开言路
广开言路 2020-12-20 22:17

I could write the following to convert an object to an integer.

Convert.ToInt32(myObject);

But I could also write

Int.Parse         


        
4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-20 23:00

    As far as I know, Convert and Parse do differ in so many ways:

    To convert means to cast an object from its original type to another type (if possible).However both objects are somehow equal in their own context for example "32" is the string version of 32 (as an integer).In some languages like Visual Basic this kind of conversion can happen implicitly.

    To parse means to accept an input (usually in the form of an string) and to translate it to an object which can be a totally different thing. Take date as an example:We can parse "20 July 2010" which is a string to a date. It means that we have to translate the provided string into a date object that has 20 as its day,7 as its month and 2010 as it year. It is obvious that this task is not a straightforward one and a logic should be in place to parse the string.

提交回复
热议问题