what is the different between Parse and Convert

Posted by Gow.net under C# on 1/23/2012 | Points: 10 | Views : 9879 | Status : [Member] | Replies : 3
what is the different between Parse and Convert and any one explain following Example

 string no = "5";
int a, b;
a = int.Parse(no);
b = Convert.ToInt32(no);

Console.WriteLine(a);
Console.WriteLine(b);

i want explanation a,b;
a = int.Parse(no);
b = Convert.ToInt32(no);


gowthaman8870226416


Responses

Posted by: Blessyjees on: 1/23/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi,

Refer this link http://www.codeproject.com/Articles/32885/Difference-Between-Int32-Parse-Convert-ToInt32-and

Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

Gow.net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hemanthlaxmi on: 1/23/2012 [Member] Starter | Points: 25

Up
0
Down
When passed a string as a parameter, Convert.ToInt32 calls int.Parse internally. So the only difference is an additional null check.

Here's the code from .NET Reflector

public static int ToInt32(string value)
{
if (value == null)
{
return 0;
}
return int.Parse(value, CultureInfo.CurrentCulture);
}

http://stackoverflow.com/questions/8192520/any-difference-between-parse-and-convert

If this helps you .
Please "Mark as Answer"

Gow.net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Naraayanan on: 1/23/2012 [Member] Starter | Points: 25

Up
0
Down
Hi ,
Convert class makes it easier to convert between the all the base types.
One Difference:
string is null
thrown error when you call int.Parse()
return 0 value when you call Convert.ToInt32()
if you want to know the Difference between Int.Parse() and Convert.ToINT32(). please test the following code:
  int a, b;

string s = null;

b = Convert.ToInt32(s);
MessageBox.Show(b.ToString());

a = int.Parse(s);
MessageBox.Show(a.ToString());


Output is same when you use your code.


Regards,
Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/
http://abaprocker.blogspot.com/

Gow.net, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response