Converting string to integer C# with Example



Converting string to integer C# with Example

There are various methods available for explicitly converting a string to an integer, such as: 
1. Convert.ToInt16(); 
2. Convert.ToInt32(); 
3. Convert.ToInt64(); 
4. int.Parse(); 
But all these methods will throw a FormatException, if the input string contains non-numeric characters. For this, 
we need to write an additional exception handling(try..catch) to deal them in such cases. 
Explanation with Examples: 
So, let our input be: 
string inputString = "10.2"; 
Example 1: Convert.ToInt32() 
int convertedInt = Convert.ToInt32(inputString); // Failed to Convert 
// Throws an Exception "Input string was not in a correct format." 
Note: Same goes for the other mentioned methods namely - Convert.ToInt16(); and Convert.ToInt64(); 
Example 2: int.Parse() 
int convertedInt = int.Parse(inputString); // Same result "Input string was not in a correct format. 
How do we circumvent this? 
As told earlier, for handling the exceptions we usually need a try..catch as shown below: 
try 
{ 
string inputString = "10.2"; 
int convertedInt = int.Parse(inputString); 
} 
catch (Exception Ex) 
{ 
//Display some message, that the conversion has failed. 
} 
But, using the try..catch everywhere will not be a good practice, and there may be some scenarios where we 
0 0 
wanted to give if the input is wrong, (If we follow the above method we need to assign to convertedInt from the 
catch block). To handle such scenarios we can make use of a special method called .TryParse(). 
 

The .TryParse() method having an internal Exception handling, which will give you the output to the out 
parameter, and returns a Boolean value indicating the conversion status (true if the  conversion  was  successful;  false 
if it failed). Based on the return value we can determine the conversion status. Lets see one Example: 
Usage 1: Store the return value in a Boolean variable 
int convertedInt; // Be the required integer 
bool isSuccessConversion = int.TryParse(inputString, out convertedInt); 
We can check The variable isSuccessConversion after the Execution to check the conversion status. If it is false 
0 0 
then the value of convertedInt will be (no need to check the return value if you want for conversion failure). 
Usage 2: Check the return value with if 
if (int.TryParse(inputString, out convertedInt)) 
{ 
// convertedInt will have the converted value 
// Proceed with that 
} 
else 
{ 
// Display an error message 
} 
Usage 3: Without checking the return value you can use the following, if you don't care about the return value 
0 
(converted or not, will be ok) 
int.TryParse(inputString, out convertedInt); 
// use the value of convertedInt 
// But it will be 0 if not converted 
 

Stacktraces 
A stack trace is a great aid when debugging a program. You will get a stack trace when your program throws an 
Exception, and sometimes when the program terminates abnormally. 

0 Comment's

Comment Form

Submit Comment