C# .NET - int.parse,int.tryparse,convert.toint16,convert.toint32,convert.toint64

Asked By madhu krishna on 23-Apr-11 03:03 AM
int.parse,
int.tryparse,
convert.toint16,
convert.toint32,
convert.toint64

what is the difference among the above 5 when should should we use them appropriately?
Reena Jain replied to madhu krishna on 23-Apr-11 03:33 AM
hi,

First i would like to provide the difference between int.parse and convert.To(s) and Int.TryParse()
  1. Convert.To(s) doesn't throw an exception when argument is null, but Parse() does. Convert.To(s) returns 0 when argument is null.

  2. Int.Parse() and Int.TryParse() can only convert strings. Convert.To(s) can take any class that implements IConvertible.Hence, Convert.To(s) is probably a http://blogs.msdn.com/b/ianhu/archive/2005/12/19/505702.aspx than Int.Parse() because it has to ask its argument what it's type is.

So the difference between the Int.Parse and Convert.ToInt32  is the manner in which null is handled. If you pass a null value to convert.ToInt32 method it will return back 0. But the same is not true with Int.Parse. If we pass null to Int.Parse method it will throw an ArgumentNullException exception.

Convert.ToInt16 => consume 1 bytes memory to store integer value
Convert.ToInt32 => consume 2 bytes memory to store integer value
Convert.ToInt64 => consume 4 bytes memory to store integer value. you can use this in place of long because there is not long datatype available in c#.net


Hope this will help you
Riley K replied to madhu krishna on 23-Apr-11 04:39 AM

Basically the Convert class makes it easier to convert between all the base types.

The Convert.ToInt32(String, IFormatProvider) underneath calls the Int32.Parse. So the only difference is that if a null string is passed to Convert it returns 0, whereas Int32.Parse throws an ArgumentNullException

Use Convert class when you convert object type that contains type in which you want to convert. For example when you execute ExecuteScalar of SqlDataReader it will return object type result but you know which type of object you expect and you use Convert.ToInt32, Convert.ToString or some other, to get the value.

Same is when you want to get SelectedItem of the combo which is of type object but contains string and you need to use Convert.ToString to get the selected value.
'type'.Parse or 'type'.TryParse can be used only when converting string which contain targeted value type.
Nikhil Mahajan replied to madhu krishna on 24-Apr-11 03:34 AM
hi..

http://msdn.microsoft.com/en-us/library/sf1aw27b.aspx converts an object to an integer and returns 0 if the value was null.

int x = Convert.ToInt32("43"); // x = 43;
int x = Convert.ToInt32(null); // x = 0;
int x = Convert.ToInt32("abc"); // throws FormatException

http://msdn.microsoft.com/en-us/library/system.int32.parse.aspx convert a string to an integer and throws an exception if the value wasn't able to convert

int x = int.Parse("43"); // x = 43;
int x = int.Parse(null); // x throws an ArgumentNullException
int x = int.Parse("abc"); // throws an FormatException

  1. Convert.To(s) doesn't throw an exception when argument is null, but Parse()does.Convert.To(s) returns 0 when argument is null.

  2. Int.Parse() and Int.TryParse() can only convert strings. Convert.To(s) can take any class that implements IConvertible.Hence, Convert.To(s) is probably a http://blogs.msdn.com/b/ianhu/archive/2005/12/19/505702.aspx thanInt.Parse() because it has to ask its argument what it's type is.

Ravi S replied to madhu krishna on 25-Apr-11 08:45 AM
Hi Madhu

Int32.parse(string)

Int32.Parse (string s) method converts the string representation of a number to its 32-bit signed integer equivalent. When s is a null reference, it will throw ArgumentNullException. If s is other than integer value, it will throw FormatException. When s represents a number less than MinValue or greater than MaxValue, it will throw OverflowException.

Convert.ToInt32(string)

Convert.ToInt32(string s) method converts the specified string representation of 32-bit signed integer equivalent. This calls in turn Int32.Parse () method. When s is a null reference, it will return 0 rather than throw ArgumentNullException. If s is other than integer value, it will throw FormatException. When s represents a number less than MinValue or greater than MaxValue, it will throw OverflowException.

Int32.TryParse(string, out int)

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, false otherwise. This method is available in C# 2.0. When s is a null reference, it will return 0 rather than throw ArgumentNullException. If s is other than an integer value, the out variable will have 0 rather than FormatException. When s represents a number less than MinValue or greater than MaxValue, the out variable will have 0 rather than OverflowException

For examples follow the link below
http://www.codeproject.com/KB/cs/AgileWare_Convert_Int32.aspx

Hope this helps you...