Attention: We are retiring the ASP.NET Community Blogs. Learn more >

int.Parse() vs. Convert.Int32()


Everytime I have to convert a string to an integer I use int.Parse() instead of Convert.Int32(). Until today, I basically recommended it because it looked more O-O to me (besides, I get the chance to pick my Java pals because int behaves more like a real class in C# ;-). But today Clemens Vasters, a fellow RD, gave me a really sound reason for doing so: int is a higher level abstraction than Int32 and, by default, higher level abstractions are friends of the (business) developer. Nice one, Clemens.

8 Comments

  • Using of convert is better than using of parse becoz convert cheks for null also

  • I'm sure you meant Convert."To"Int32(). I'm developing with mono and a simple editor (and mcs --debug) here, and used this blog-entry as a documentation resource... since the mono-doc is still under construction.

  • What about performance? If you run code analysis (VSTS version of VS 2005) it gives a warning for every int.Parse operation.

  • I have db column of datatype SMALLINT. Every time when I want to read the same in C#(Widnows application) I am getting exceptions when casting it using Int32(). Does there any best practise to accomplish this ?

  • Convert:- Converts a base data type to another base data type.
    You have to judge which one is better for your application. Have a look on the following code.
    char _charMy = '8';
    Response.Output.Write("Convert.ToInt32: " + Convert.ToInt32(_charMy));
    Response.Write("
    ");
    Response.Output.Write("int.Parse: " + int.Parse(_charMy.ToString()));

  • One question related work on 86x and 64x system.
    'int' on 86x system is Int32
    but same 'int' on 64x is 64. right?
    If I write code on 86x and deploy on 64x server will I face any issue with this approach?

  • C# is a Strongly Typed Language, int is a Signed 32-bit integer (-2,147,483,648 to 2,147,483,647) for both the platform (x86 and x64)

  • Convert.ToInt32() call Int32.Parse(), so Convert is a bit easier than Parse. Use Parse is better only for globalizzation

Comments have been disabled for this content.