Click here to Skip to main content
15,900,815 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hii team ,

I am very much confused between three things as requested
When to cast , when to parse and when to convert ?

For example

(int) obj.value;



please tell me when to use this things , and what is the actual difference between these three

Parse when you have a string containing a representation of a value. For example, when you have a user entering data into a TextBox and you need to use it as a number:
int value = int.Parse(myTextBox.Text);

Or preferably, use TryParse instead:
int value;
if (int.TryParse(myTextBox.Text, out value))
   ... use value
   { input error to user

Convert is the equivalent of Parse for strings, but it doesn't have a TryParse version, and it's slightly slower because it has to decide what type the parameter is: so if you have an object value to convert to an int Convert.ToInt32 will sort out the type and do it.

Casting does not do the same job: it's a syntactic sugar for "I know what this is, just make it so." - it doesn't change the actual object, it just changes the type of it (with minor exceptions, such as casting from a double to an int: that truncates the double and throws away the fractional part). But it won't change an object type unless they are castable: which means casting a derived type to an base type, or if an explicit cast operator exists (such as double -> int and so forth). You can't use a cast to change a string to an int!

1) If you have a string, use Parse, or TryParse (or TryParseExact)
2) If you have an object and you don't necessarily know what type it is, use Convert
3) If you do know what type it is and just need to refer to it in a different way, cast it.
Share this answer
Torakami 15-Feb-14 12:50pm    
Nice explanation buddy ... Thanks a lot .. i am very clear with my confusion now ..
OriginalGriff 15-Feb-14 13:28pm    
You're welcome!
You need to cast if you have, for example, an int that's stored in an object, byte, short ... For example, if you stored an int in a session variable, then you need to cast:
int yourInt = (int) Session["yourInt"];

For the difference between int.Parse and Convert.ToInt32, please see:[^]
Use one of these methods if you have a string, which is just an integer represented as a string, for example:
string str = "100";
int i = Convert.ToInt32(str); // or int.Parse
// Only do this if you are 100% sure that it's possible to convert the string to an int. If you are not 100% sure, use int.TryParse

If you have user input (or if you are not 100% sure that you can convert a string to an int), then you should use neither casting, neither int.Parse and neither Convert.ToInt32. You need to use int.TryParse[^], because TryParse checks first whether it's possible to parse it to an int:
string str = "You can't convert this to an int";
int i;
if (int.TryParse(str, out i))
   // successfully converted to an int, you can find the value in the variable i
   // couldn't convert string to int

If you would use Convert.ToInt32 or int.Parse for the above example, then your program would throw an exception.
Share this answer
Share this answer
Torakami 15-Feb-14 12:50pm    
Dude ... even i can do google ... i asked here to understand in much simpler way ..
Gitanjali Singh 15-Feb-14 12:53pm    
These links also have better explanation.Glad to know that you get the answer.

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