Back to Basics : Difference Between int.Parse() and int.TryParse()

Back to Basics : Difference Between int.Parse() and int.TryParse()

What is the difference between int.Parse() and int.TryParse() ? Or how the int.parse() is differ from int.TryParse() ? – yet another frequently  asked question in interview for the beginners, and I have seen confusion while answering this.  Let’s try to understand using simple example. To answer this in a sort way, the int.Parse() and int.TryPrase() methods is used to convert a string representation of number to an integer.  In case of the string can’t be converted the int.Parse() throws an exceptions where as int.TryParse() return a bool value, false.

Let’s try to understand with a simple example as shown in below.

image

In this case the string val, would be converted into integer without any error or exception; and that is what is intended with int.Parse() method.

The int.Prase() method throws three different types of exceptions depends on the data provided.

  • If parameter value is null, then it will throw ArgumentNullException
  • If parameter value is other than integer value or not in proper format, it will throw FormatException.
  • if parameter value is out of integer ranges, then it will throw  OverflowException.

image

 

Incase of  int.TryParse() method, when it converts the string representation of an number to an integer;  it set the the out variable with the result integer and returns true if successfully parsed, otherwise false.

image

Keep in mind in case of int.TryParse(), in case there won’t be any exception. Either it is for null parameter, incorrect format or out of value integer. Everytime, the result value would be set to 0 and method would return false.

 

image

image

 

Hope this will give you a clear picture now. Let’s put them in side by side !

image

Which one is best ? In my opinion, int.TryParse() will be the better way as it handles the exceptions by it’s own and you can just focus on the values.  Again, this will totally depends in which context you want to use it. if you want to know when it is failing and why it is then you should go for int.parse.

 

Want to catch up with work and access your important Windows Application and software as you travel? It’s possible with  a cloud desktop from CloudDesktopOnline.com . Also, for more hosted Microsoft applications such as Exchange, SharePoint, Dynamics CRM, Project Server and more, try Apps4Rent

 

Well, this is not the end, to make the discussion more interesting you may asked another question during this overall discussion.  So how they differ with Convert.ToInt32() ?  I will leave this up to you now to explore this …

Hope this helps !!

Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.

3 Comments to “Back to Basics : Difference Between int.Parse() and int.TryParse()”

Comments are closed.