Loading ...

Difference Between Int32.Parse, Convert.ToInt32 and Int32.TryParse | CodeAsp.Net

Difference Between Int32.Parse, Convert.ToInt32 and Int32.TryParse

  • 0
    /5
    Avg: 0/5: (0votes)
  • (3)
  • (5524)
Published: 9/5/2010 by Raghav Khunger

Int32. Parse (string)


Int32.Parse (string str) method converts the string representation of a number to its 32-bit signed integer equivalent. It takes a string and tries to extract an integer from it and returns the integer. When s is a null reference, it will throw ArgumentNullException. If str is not an integer value, it will throw FormatException. When str represents a number less than MinValue(−2,147,483,648) or greater than MaxValue(+2,147,483,647), it will throw OverflowException. For example:

 

string str1 = "123";
string str2 = "123.45";
string str3 = "12312312356456456456456456456456";
string str4 = null;


int resultValue;

resultValue = Int32.Parse(str1); //-- 123
resultValue = Int32.Parse(str2); //-- FormatException
resultValue = Int32.Parse(str3); //-- OverflowException
resultValue = Int32.Parse(str4); //-- ArgumentNullException



Convert.ToInt32(string)


Convert.ToInt32(string str) method converts the specified string representation of 32-bit signed integer equivalent. Convert.ToInt32 underneath calls the Int32.Parse. The only difference is that if a null string is passed to Convert it returns 0, whereas Int32.Parse throws an ArgumentNullException. If str is other than integer value, it will throw FormatException. When s represents a number less than MinValue(−2,147,483,648) or greater than MaxValue(+2,147,483,647), it will throw OverflowException. For example:

string str1 = "123";
string str2 = "123.45";
string str3 = "12312312356456456456456456456456";
string str4 = null;


int resultValue;


resultValue = Int32.Parse(str1); //-- 123
resultValue = Int32.Parse(str2); //-- FormatException
resultValue = Int32.Parse(str3); //-- OverflowException
resultValue = Int32.Parse(str4); //-- 0


Int32.TryParse(string, out int)


This method is available in C# 2.0 and above. Int32.Parse(string, out int) method converts the specified string representation of 32-bit signed integer equivalent to out variable, and returns true if it is parsed successfully else false. When input string   is a null reference, it will return 0 rather than throw ArgumentNullException as it was coming in above two methods. If input string  is other than an integer value, the out variable will have 0 rather than FormatException as it was coming in above two methods. When input string  represents a number less than MinValue or greater than MaxValue, the out variable will have 0 rather than OverflowException as it was coming in above two methods. For example

 

string str1 = "123";
string str2 = "123.45";
string str3 = "12312312356456456456456456456456";
string str4 = null;


int resultValue;
bool isParsed;
isParsed =Int32.TryParse(str1, out resultValue); //isParsed =>true; result => 123

isParsed =Int32.TryParse(str2, out resultValue); //isParsed => false; result => 0
isParsed =Int32.TryParse(str3, out resultValue); //isParsed => false; result => 0
isParsed =Int32.TryParse(str4, out resultValue); // isParsed => false; result => 0


So from above you came to know about s several different ways to extract integers from strings in
.Net. You should therefore use the method that better suits your scenario. If you've got a string, and you expect it to always be an integer use Int32.Parse.If you are expecting input other than integer use Convert.ToInt32 and if you don’t want any exception you can go for Int32.TryParse

 

Comments (3)

  
mohit
mohit kumar said:
Thanks you for nice Article.
12/14/2009
 · 
 
by
  
nxminh
X Minhx said:
show, thank you this very important infomations
<!--
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0in;
margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:12.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:"Times New Roman";}
@page Section1
{size:8.5in 11.0in;
margin:1.0in 1.25in 1.0in 1.25in;
mso-header-margin:.5in;
mso-footer-margin:.5in;
mso-paper-source:0;}
div.Section1
{page:Section1;}
-->

-->




but i have one question : i have 1 object is containt number value object a = 1;when convert to number I have to us which methord ??? int nValue = Int32. Parse (a.toString());or int nValue = Convert.ToInt32(a;  
8/19/2010
 · 
 
by
  
Karan
Karan Singh said:
nice article with facts on the integer parse.
10/19/2010
 · 
 
by

Top articles

Quick Vote

What kind of email newsletter would you prefer to receive from CodeAsp.Net?