It's often require to parse a string value to another data type, like integer, and there exists a confusion among the developers whether to use the Parse method or the TryParse method that the framework provides to us.

 

In this post, we will discuss the difference between each of those two methods and guide you to understand and pick the preferable one. Continue reading to know more.

 

Parse or TryParse? Which one should I use?

 

It's a common interview question to ask a developer the difference between the int.Parse and int.TryParse methods. In most of the cases, I noticed that the developers get confused while answering this question. I often seen, while reviewing the code, people uses the int.Parse as it directly returns the value. But, there could be cases when the said method will fail and throw Exception. Let's discuss in more detail.

 

As the name itself says that the Parse method parses the string to return you the value, whereas the TryParse first tries to parse it and returns the original value if it succeeds. First one takes a single parameter as string and the other takes two parameters: one as an input string and the other as the out value. If TryParse succeeds, it returns true and provides you an option to check whether the parsing was successful.

Let's learn about int.Parse(...)

It's a common tendency to call the int.Parse(...) method to parse a string to an integer. But wait! Did you ever check what happens when you pass a null, any invalid string or a large number? Let's discuss each one of them, so that, you can get a clear understanding.

 

When you pass a null string as input parameter to int.Parse(...), it will throw you an ArgumentNullException:

 

string value = null;
int outValue = int.Parse(value); // throws ArgumentNullException
 

 

When you pass a correct integer value as string, it will parse it and return you the value:

 

string value = "999";
int outValue = int.Parse(value); // works fine, returns 999
 

 

When you pass a double value as string, instead of an integer, the outcome of it is a FormatException:

 

string value = "999.99";
int outValue = int.Parse(value); // throws FormatException
 

When it is unable to parse the string (like the below text) to integer, a FormatException will trigger:

 

string value = "kunal";
int outValue = int.Parse(value); // throws FormatException
 

 

When you pass a very large integer value as string to the method, it will throw OverflowException as it can't parse it to integer:

 

string value = "9999999999999999";
int outValue = int.Parse(value); // throws OverflowException
 

Let's learn about int.TryParse(..., ...)

Unlike the Parse method, int.TryParse(..., ...) does not directly return you the value. Instead, it returns whether the parsing was successful or not. The second parameter passed to this method is an out parameter, that gets filled by the method with the parsed value.

 

When you pass a null value to TryParse, instead of throwing ArgumentNullException, it returns false and fills the out value to '0' (zero):

 

string value = null;
int outValue = 0;
bool status = int.TryParse(value, out outValue); // outValue = 0
Console.WriteLine("Status: {0}, Value: {1}", status, outValue);
 

 

When you pass a correct integer value as string, it returns true and fills the out value with the parsed result:

 

string value = "999";
int outValue = 0;
bool status = int.TryParse(value, out outValue); // outValue = 999
Console.WriteLine("Status: {0}, Value: {1}", status, outValue);
 

 

Passing a double, does not throw FormatException. Rather, it returns false and fills the out value with '0' (zero):

 

string value = "999.99";
int outValue = 0;
bool status = int.TryParse(value, out outValue); // outValue = 0
Console.WriteLine("Status: {0}, Value: {1}", status, outValue);
 

 

Similarly, if you pass a non-integer value, it returns false and fills out value with '0' (zero):

 

string value = "kunal";
int outValue = 0;
bool status = int.TryParse(value, out outValue); // outValue = 0
Console.WriteLine("Status: {0}, Value: {1}", status, outValue);
 

 

When you pass a large integer number as string, which it can't parse, will return false instead of throwing OverflowException and will fill the out value with '0' (zero):

 

string value = "9999999999999999";
int outValue = 0;
bool status = int.TryParse(value, out outValue); // outValue = 0
Console.WriteLine("Status: {0}, Value: {1}", status, outValue);
 

 

Based on the returned value, you can check whether the parsing was successful. If succeeded, you can use the out value in subsequent lines of code. A point to note that, you need to initialize the out value before passing it to the TryParse method.

End Note

I hope that, you understood the difference between Parse and TryParse methods from the above post. Which one do you prefer now while writing code to parse a numeric string to number? It's TryParse method, for sure! Do let me know, if you still have any doubts/queries.

 

Another point to note that, it's applicable to all conversion from string to integer, double, long etc. For example: if you want to parse a string to double, call double.TryParse(..., ...); to convert a string to long, call long.TryParse(..., ...) similarly.

 

 

Have a question? Or, a comment? Let's Discuss it below...

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.