Uses Of Int.Parse, Convert.ToInt32, And int.TryParse

Introduction

Here, we are going to see the uses of and differences among Int.Parse, Convert.ToInt32, and int.TryParse.

Prerequisite
  • Visual Studio 
  • Basic understanding of C#
 Article Flow
  1. Uses of int.Parse
  2. Uses of Convert.ToInt32
  3. Uses of int.TryParse
  4. Differences between int.Parse, Convert.ToInt32, and int.TryParse.
Uses of int.Parse

int.Parse() is a method to convert the given input into integer. Let's have an example to see this unique functionality.

Syntax

int.Parse(string s) 

Examples

Here, first we will try converting the string (which contains only numbers) to integer.
  1. string input1 = "101";  
  2. Console.WriteLine("int.Parse:(String to integer) : "+int.Parse(input1));  
Now, run and see the output.



So, it's successfully converting the input string's numbers to integers.

Now, let's try with converting the integer to integer.
  1. int input2 = 101;  
  2. Console.WriteLine("int.Parse:(integer to integer) : " + int.Parse(input2));  
OMG! It's not converting the pure integer to integer; see the below error.

 

So, we need to convert the each input into string and aftwards only, we can convert the given input into integer.
  1. int input2 = 101;  
  2. Console.WriteLine("int.Parse:(Converted String to integer) : " + int.Parse(input2.ToString()));  
Here, I used the input2.ToString(). We may convert the given as string by using the Convert.String() als. If you want to know the differences, refer the below link.
Now, run and see the output.

 

Now, let's try with converting the String (which contains characters) to integer.
  1. string input3 = "Am String";  
  2. Console.WriteLine("int.Parse:(Converted String to integer) : " + int.Parse(input3));  
Now, run and see the output. Here, we are getting the formatException so we can't convert the string which contains alphabet to integer.

 

Let's try with converting the Null to integer.
  1. string input4 = null;  
  2. Console.WriteLine("int.Parse:(Converted String to integer) : " + int.Parse(input4));  
Now, run and see the output. Here, we are getting the ArgumentNullException.

 

Let's try with converting the String (which contains double/float value) to integer.
  1. string input4 = "855555555555555555555.00052555555555555";  
  2. Console.WriteLine("int.Parse: " + int.Parse(input5));  
Now, run and see the output. We are getting the FormatException.



Let's try with converting the String that contains long value, to integer.
  1. string input6 = "85555555555555555555500052555555555555";  
  2. Console.WriteLine("int.Parse:(Long to integer) : " + int.Parse(input6));  
Now, run and see the output. Here, we are getting OverflowException.

 

I hope you got clear knowledge about Int.Parse. It's used to convert the input into integer. The input integer should be a string which contains only numbers.

Use of Convert.ToInt32

Convert.ToInt32() is a method to convert the given input into integer. Let's have an example to see this unique functionality.

Syntax
Convert.ToInt32(bool value)

Examples

Here first, we will try with converting the boolean to integer.
  1. bool input00 = false;  
  2. Console.WriteLine("Convert.ToInt32:(bool to integer) : " + Convert.ToInt32(input00));  
We know very well that false represents 0 and true represents 1. Okay, now let's see by running this code.

 

Try with converting the string (which contains only number) to integer.
  1. string input11 = "101";  
  2. Console.WriteLine("Convert.ToInt32:(String to integer) : " + Convert.ToInt32(input11));  
Now, run and see the output. It's successfully converting the give input (which contains only number) to integer.

 

Let's try converting the integer to integer.
  1. int input22 = 101;  
  2. Console.WriteLine("Convert.ToInt32:(integer to integer) : " + Convert.ToInt32(input22.ToString()));  
Now, run and see the output.

 

Let's try converting the String (which contains characters) to integer.
  1. string input33 = "Am String";  
  2. Console.WriteLine("Convert.ToInt32:(String to integer) : " + Convert.ToInt32(input33));  
Now, run and see the output. We know very well, we can not convert the string (which contains alphabet) to integer, so we are seeing the format exception.

 
 
Let's try converting Null to integer.
  1. string input44 = null;  
  2. Console.WriteLine("Convert.ToInt32:(null to integer) : " + Convert.ToInt32(input44));  
Now, run and see the output. It's handling null value and returning 0.

 

Let's try converting the String (which contains double/float value) to integer.
  1. string input5 = "855555555555555555555.00052555555555555";  
  2. Console.WriteLine("Convert.ToInt32: " + Convert.ToInt32(input5));  
Now, run and see the output.

 
 
Let's try converting the String (which contains long value) to integer.
  1. string input66 = "85555555555555555555500052555555555555";  
  2. Console.WriteLine("Convert.ToInt32:(Long to integer) : " + Convert.ToInt32(input66));  
Now, run and see the output.

 

Now, we got clear knowledge about Convert.Toint32. It's used to convert the input into integer and bool. The input integer should be an integer; if it's null it will return 0; if it's string, it should only contain the number. 

Uses of int.TryParse

int.TryParse(input,out) is a method to convert the given input into integer, and the tryparse method is always used with out parameter to display default value. Let's have an example to see its unique functionality. 

Syntax
int.TryParse(string s, out int result); 

Parameter s represents input value, out represents that it's the default value to return while tryparse failed to convert the input as well as the tryparse never fall into the exception because of out param.

Okay, now we move to the examples.
 
First, we will try with converting the boolean to integer.
  1. int defaultout;  
  2. bool input000 = false;  
  3. int .TryParse(input000 .ToString(), out defaultout);  
  4. Console .WriteLine("int.TryParse:(bool to integer) : " + defaultout);  
Now, run and see the output. There is no issue here. It's also converting the bool value to integer.

 
Let's try with converting the string (which contains only number) to integer.
  1. string input111 = "101";  
  2. int .TryParse(input111, out defaultout);  
  3. Console .WriteLine("int.TryParse:(String to integer) : " + defaultout);  
Now, run and see the output.

 

Let's try with converting the String (which contains characters) to integer.
  1. string input222 = "am STring";  
  2. int .TryParse(input222, out defaultout);  
  3. Console .WriteLine("int.TryParse:(String to integer) : " + defaultout);  
Now, run and see the output. Here, actually tryparse cannot convert the input which contains alphabet but it doesn't throw the exception. So, it returns the default value of our parameter value. The default out parameter value is 0 (zero).

 
 
Let's try converting Null to integer.
  1. string input333 = null;  
  2. int .TryParse(input333, out defaultout);  
  3. Console .WriteLine("int.TryParse:(Null to integer) : " + defaultout);  
Now, run and see the output. Here, the tryparse failed to convert the null value to integer even it doesn't throw the exception and returns the default out param value.

 

Let's try with converting the String (which contains double/float value) to integer.
  1. string input444 = "855555555555555555555.00052555555555555";  
  2. int .TryParse(input444, out defaultout);  
  3. Console .WriteLine("int.TryParse:" + (defaultout));  
Now, run and see the output, here also it's failed to convert but it dosen't throw the exception

 

Let's try with converting the String (which contains long value) to integer.
  1. string input5555 = "85555555555555555555500052555555555555";  
  2. int .TryParse(input5555, out defaultout);  
  3. Console .WriteLine("int.TryParse{Long to int}:" + (defaultout));  
Now, run and see the output. Here also, the same thing happens - it returns the default value.

 

Now, we got clear knowledge about int.TryParse. This is used to convert the input into integer, the input value maybe anything. If it's failed to convert given input, means it will return the default out param value as result, and will never throw an exception.

Differences Between int.Parse, Convert.ToInt32, and int.TryParse

We have seen the uses of int.parse, convert.toint32 and int.tryparse and we got more detail about these all. Now, let's list all points to find the differences.

  int.Parse Convert.ToInt32int.TryParse 
 Syntax int.Parse(string s) Convert.ToInt32(bool value) int.TryParse (string s,out int result)
 string str1 = "2017"; 2017 2017 2017
 string str2 = null; ArgumentNullException 0 Failed to Convert but returns 0 and doesn't throw the exception
string str3 = "2017.111111"; FormatException FormatException Failed to Convert but returns 0 and doesn't throw the exception
string str4 = "201717171717777777777777777777777777"; OverflowException OverflowException Failed to Convert but returns 0 and doesn't throw the exception
bool input00 = false; Not Supporting to convert 0 Failed to Convert but returns 0 and doesn't throw the exception

Summary

In this article, we learned the uses of int.Parse,Convert.Toint32 and int.TryParse, along with their differences. I hope you enjoyed this article. Your valuable feedback is always welcome.


Similar Articles