Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Casting and Parsing

I have been working on some code for a while. for now i have just used the keywords as-is without actually understanding them. So here is my question

What is the difference between Casting and Parsing?

UserAdapter.GetIdAndUserTypeByEmailAndPassword(Email, Password).Rows[0]["UserType"] as String--> is this Casting or parsing?

(String) UserAdapter.GetIdAndUserTypeByEmailAndPassword(Email, Password).Rows[0]["UserType"] -->is this Casting or parsing?

UserAdapter.GetIdAndUserTypeByEmailAndPassword(Email, Password).Rows[0]["UserType"].ToString()
What is the difference bewtween x.ToString() and (String) x?

like image 610
prometheuspk Avatar asked Dec 22 '22 14:12

prometheuspk


1 Answers

What is the difference between Casting and Parsing?

Those are unrelated.

Casting is changing the type of the variable.

Parsing is 'examining' the string and assigning its logical value to some variable.

(ADDITION: Well, they are related in a sense, because from far far away both can serve to 'convert' data, however, data is really converted ONLY in case of parsing)

UserAdapter.GetIdAndUserTypeByEmailAndPassword(Email, Password).Rows[0]["UserType"] as String

is this Casting or parsing?

This is special kind of casting that will not fail if types aren't convertible (look here), but will get you null.

(String) UserAdapter.GetIdAndUserTypeByEmailAndPassword(Email, Password).Rows[0]["UserType"]

is this Casting or parsing?

This again is casting, but will throw an exception if expression isn't of type string.

What is the difference bewtween x.ToString() and (String) x?

x.ToString() will try to call ToString() on the object x.

(String) x will try to cast x to string, and will fail if x isn't string.

like image 60
Daniel Mošmondor Avatar answered Jan 05 '23 01:01

Daniel Mošmondor