Search content within the blog

Wednesday, April 29, 2009

Difference between int.parse and convert.ToInt in asp.net

Int.Parse cannot convert null values to some default value like zero as Convert.ToInt does. so it is always safe to use Convert.ToInt instead of Int.Parse.

The code is self explanatory....

protected void Page_Load(object sender, EventArgs e)
{
string s = null;
Response.Write("Output: "+Convert.ToInt32(s).ToString()+"<br>");
try
{
Response.Write(Int32.Parse(s).ToString());
}
catch
{
Response.Write("Output : Int32.Parse is in try catch block as it cannot convert null to zero as Convert.ToInt32 does.");
}
}

No comments:

Post a Comment