Click here to Skip to main content
15,900,815 members
Please Sign up or sign in to vote.
1.44/5 (3 votes)
See more:
I am reading a BOOK about c# and i have come across some kinda weird concepts,
at the time i am confused about Casting and Parsing. I know that Casting and Parsing are helping me to achieve the same thing but.
1) When i use eachone?
2) Whats their differences?

Simple examples would be usefull

Thanks in advance Black Hat
Posted

1 solution

Casting is changing the logical type of a value from one type to another, and (normally) doesn't alter information (exceptions exist for casting between number types: double to int for example will throw away data to the left of the decimal point, int to byte would throw away all large values and effectively take the modulus of the original integer and 256). You cannot cast from one type to another unless there one is derived from the other, or a specific cast operator have been defined which takes the two types. For example, you can't cast from a string "1234" to an integer 1234

Parsing is the process of converting the string representation of a type into an instance of the type: so if the user types the string "1234" into a text box and you want to multiply it my two, you parse the string to an integer, and you can then multiply the integer value and get 2468 as a result.
Parse methods exist for many standard types: int, short, byte, float, double, DateTime, TimeSpan, etc.
 
Share this answer
 
Comments
GeorgeZiozas 21-Jun-15 5:53am    
So since parsing can be excecuted between all the data types why we have cast?
OriginalGriff 21-Jun-15 5:59am    
parsing can't be executed between all datatypes!
"Parsing is the process of converting the string representation of a type into..."
You can only parse a string into a different type.
You can't parse a double to a int! :laugh:
Member 14226188 11-Apr-19 9:42am    
Same question I was also facing, but now clear!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900