Tuesday, August 25, 2009

What is the difference between int.parse() and convert.int32()

If we want to convert a string value (Lets say we have a string “23”) to integer we have 2 options. One is to use the Int.Parse method and other is to use the Convert.ToInt32.

The real query with every one was what is the difference between the two. The answer is null handling. The difference between the 2 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.

Although Convert.ToInt32 method does not throw an exception but it can have a big drawbacks. If you use it on a query string value(where u are also expecting the value 0) then the Convert.ToInt32 might cause programmatic error.

######################################################################################


The difference lies in the way both handles NULL value.

When encountered a NULL Value, Convert.ToInt32 returns a value 0. On other hand,Parse is more sensitive and expects a valid value. So it would throw an exception when you pass in a NULL.

string stringInt = "01234";

int iParse = int.Parse(stringInt);

int iConvert = Convert.ToInt32(stringInt);


##################################################################################3

string MyString = "12345";
int MyInt = int.Parse(MyString);
MyInt++;
Console.WriteLine(MyInt);
// The result is "12346".

#################################################################################
int.Parse for Integer Conversion in C#
--------------------------------------

1. Using int.Parse

First, here we see the int.Parse method. int.Parse is the simplest method, and is also the author's favorite for many situations. It throws exceptions on invalid input, which can be slow if they are common. It is does not contain any internal null checks.

=== Example program that uses int.Parse (C#) ===

using System;

class Program
{
static void Main()
{
// Convert string to number.
string text = "500";
int num = int.Parse(text);
Console.WriteLine(num);
}
}

=== Output of the program ===

500


2. Using Convert.ToInt32

Third, we look at the Convert.ToInt32 method. Convert.ToInt32, along with its siblings Convert.ToInt16 and Convert.ToInt64, is actually a static wrapper method for the int.Parse method. It can be slower than int.Parse if the surrounding code is equivalent.

=== Example program that uses Convert.ToInt32 (C#) ===

using System;

class Program
{
static void Main()
{
// Convert 'text' string to an integer with Convert.ToInt32.
string text = "500";
int num = Convert.ToInt32(text);
Console.WriteLine(num);
}
}

=== Output of the program ===

500


3. Which method should I use?

The it's recommendation is to use int.Parse when your input will be valid, as it makes for simpler calling code. It isn't always perfect, but it is a winner. On the other hand, use int.TryParse when you will be dealing with corrupt data.


Good Website : http://dotnetperls.com/datetime-1

2 comments:

बाल भवन जबलपुर said...

दीप की स्वर्णिम आभा
आपके भाग्य की और कर्म
की द्विआभा.....
युग की सफ़लता की
त्रिवेणी
आपके जीवन से ही आरम्भ हो
मंगल कामना के साथ

बाल भवन जबलपुर said...

दीप की स्वर्णिम आभा
आपके भाग्य की और कर्म
की द्विआभा.....
युग की सफ़लता की
त्रिवेणी
आपके जीवन से ही आरम्भ हो
मंगल कामना के साथ