Friday, 6 October 2017

Difference between int.Parse,Convert.ToInt32 and int.TryParse

Difference between int.Parse , Convert.ToInt32 and int.TryParse

int.Parse(string s)

  • Simply, int.Parse (string s) method converts the string to integer.
  • If string s is null, then it will throw ArgumentNullException
  • If string s is other than integer value, then it will throw FormatException.
  • If string s represents out of integer ranges, then it will throw OverflowException.
Example:

  1. class Program
  2. {
  3.         static void Main(string[] args)
  4.         {
  5.             string  Num1="123";
  6.             string  Num2=null;
  7.             string  Num3="123.6743";
  8.             string  Num4="34909564545464509123409"; 
  9.             int finalResult;
  10.             finalResult = int.Parse(Num1); //success
  11.             finalResult = int.Parse(Num2); // ArgumentNullException
  12.             finalResult = int.Parse(Num3); //FormatException
  13.             finalResult = int.Parse(Num4); //OverflowException 
  14.         }
  15.     } 

Convert.ToInt32(string s)
  • Simply, Convert.ToInt32(string s) method converts the string to integer.
  • If string s is null, then it will return 0 rather than throw ArgumentNullException.
  • If string s is other than integer value, then it will throw FormatException
  • If string s represents out of integer ranges, then it will throw OverflowException.

Example:

  1. class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             
  6.             string  Num1="123";
  7.             string  Num2=null;
  8.             string  Num3="123.6743";
  9.             string  Num4="34909564545464509123409"; 
  10.             int finalResult;
  11.             finalResult = Convert.ToInt32(Num1); 
  12.             finalResult = Convert.ToInt32(Num2); 
  13.             finalResult = Convert.ToInt32(Num3); 
  14.             finalResult = Convert.ToInt32(Num4); 
  15.         }
  16.     }

int.TryParse(string s,Out)

  • Simply int.TryParse(string s,out int)method converts the string to integer out variable and   returns true if successfully parsed, otherwise false. 
  • If string s is null, then the out variable has 0 rather than throw ArgumentNullException. 
  • If string s is other than integer value, then the out variable will have 0 rather than             FormatException.
  •  If string s represents out of integer ranges, then the out variable will have 0 rather than   throw OverflowException.

Example
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         
  6.             string  Num1="123";
  7.             string  Num2=null;
  8.             string  Num3="123.6743";
  9.             string  Num4="34909564545464509123409"; 
  10.         int finalResult;
  11.         bool output;
  12.         output = int.TryParse(Num1, out finalResult); // 9009
  13.         output = int.TryParse(Num2, out finalResult); // 0
  14.         output = int.TryParse(Num3, out finalResult); // 0
  15.         output = int.TryParse(Num4, out finalResult); // 0 
  16.     }
  17. }

What is the difference between Convert.ToInt32 and (int)?

(int)foo is simply a cast to the Int32 (int in C#) type. This is built into the CLR and requires that foo be a numeric variable (e.g. floatlong, etc.) In this sense, it is very similar to a cast in C.
Convert.ToInt32 is designed to be a general conversion function. It does a good deal more than casting; namely, it can convert from any primitive type to a int (most notably, parsing a string). You can see the full list of overloads for this method here on MSDN.

No comments:

Post a Comment