CALL 1.800.975.6814    LIVE CHAT

@dailyrazor Blog

int.Parse() vs int.TryParse() – All You Need To Know

int.parse vs int.tryparse

At one point or another, every new .NET developer using C# (CSharp) will come across the int.Parse() and int.TryParse method and wonder what is the difference between the two. The debate over int.Parse() vs int.TryParse() is constant over social media with different C# programmers preferring one method over the other.

In this brief post, we’re going to tell you all you need to know about all the differences between int.Parse() and int.TryParse(). So read on and don’t miss a sentence.

Let’s go!

 

[SPECIAL OFFER]:

Fastest ASP.NET Hosting with FREE MS SQL Server

[BENEFITS]:

  • FREE 1-Click Install of Open Source Apps, Blog, CMS, and much more!
  • Support for ASP.NET, .NET Core, MS SQL Server, MS Access, etc!
  • Multi-Domain Hosting & 99.9% Uptime Guarantee!
  • Super Fast Servers with 24/7/365 Technical Support!

Click here to access this [SPECIAL OFFER]

 

What Is int.Parse() and int.TryParse()?

First of all, let’s get the basic definitions of both functions out of the way. Now, int.Parse() is a method that allows you to programmatically convert a number’s string representation to its specific 32-bit signed integer alternative. Likewise, int.TryParse() method also perform the same function.

So you can deduce that both methods are used when you want to convert a number represented as a string to an integer.

Difference Between int.Parse() and int.TryParse()

However, given that both methods perform the same function, you may be wondering what then is the difference between int.Parse() and int.TryParse()?

The difference in both methods is in the way they react when the string you are trying to convert to integer can’t be converted to an integer. And in such a circumstance, the int.Parse() method will throw an exception whereas the int.TryParse() will return a boolean value of false.

 

[SPECIAL OFFER]:

Fastest ASP.NET Hosting with FREE MS SQL Server

[BENEFITS]:

  • FREE 1-Click Install of Open Source Apps, Blog, CMS, and much more!
  • Support for ASP.NET, .NET Core, MS SQL Server, MS Access, etc!
  • Multi-Domain Hosting & 99.9% Uptime Guarantee!
  • Super Fast Servers with 24/7/365 Technical Support!

Click here to access this [SPECIAL OFFER]

 

Examples of int.Parse() vs int.TryParse() in C#

Let’s take a look at as some code example of int.Parse() and int.TryParse() in action.

Using int.Parse() Example


using System.IO;
using System;
class Program {
   static void Main() {
      int intVal;
      string strVal = "505";
      intVal = int.Parse(strVal);
      Console.WriteLine("Here is a numeric representation of strVal: "+intVal);
   }
}

When the above code is executed and run, you can expect an output value of:


Here is a numeric representation of strVal: 505

 

But bear in the mind that if the strVal cannot be converted to an integer, then the int.Parse() method will through an exception. Therefore, you must make provisions within your code to catch and handle such exceptions if and when thrown.

Using int.TryParse() Example

Now, let’s try an example of using the int.TryParse() method to convert a string representation of a number value to an integer.

using System.IO;
using System;
class Program {
   static void Main() {
      bool boolVal;
      int intVal;
      string strVal = "504";
      boolVal = int.TryParse(strVal, out intVal);
      Console.WriteLine("Here is a numeric representation of strVal: "+boolVal);
   }
}

And as you can figure, when the above code is executed and run, you can expect an output value of:


Here is a numeric representation of strVal: True

 

If the convert failed, you’d get a value of False, which indicates that the string cannot be converted to an integer. So you can see that the int.TryParse() method is an excellent way to try and see if the string value in question can be converted to an integer or not.

 

[SPECIAL OFFER]:

Fastest ASP.NET Hosting with FREE MS SQL Server

[BENEFITS]:

  • FREE 1-Click Install of Open Source Apps, Blog, CMS, and much more!
  • Support for ASP.NET, .NET Core, MS SQL Server, MS Access, etc!
  • Multi-Domain Hosting & 99.9% Uptime Guarantee!
  • Super Fast Servers with 24/7/365 Technical Support!

Click here to access this [SPECIAL OFFER]

 

int.Parse() vs int.TryParse() Performance Considerations

Another essential question that new C# developers ask is which of the two methods is faster in terms of performance and reliability. And the consensus across the board is that when you are not sure that the string you’re trying to convert to integer is convertible, then common sense dedicates that you use the int.TryParse() as it will be a much more reliable and faster option in such a scenario.

Final Thoughts

In summary, you now know that both int.Parse() and int.TryParse() methods are used to convert a string to an integer and that the said string should be a representation of a number. Therefore, don’t try converting a string representing a text to an integer, it won’t work.

Moreover, the int.TryParse() method provides a more reliable option because it doesn’t throw an exception if the string value you’re trying to convert is not convertible. On the other hand, int.Parse() gives you a faster alternative if you are sure the string value is a number, and you don’t need to try to convert or catch any exception.