Wednesday, 9 December 2015

What is the difference between convert.ToInt32 and int32.Parse ?

string convertToInt = "78";
string nullString = null;
string maxValue = "93333332222222224444442222222222";
string formatException = "99.99";

int ParseResult;

Int32.Parsh(String Str)
Convert.ToInt32(String Str)
Int32.Parse(str) method converts the string representation of a number to integer 32 bit.
Exe.
// It will perfectly convert interger
parseResult= int.Parse(convertToInt);

Convert.ToInt32(str) method converts the string representation of a number to integer 32 bit.
Exe.
//It will perfectly convert integer
parseResult= Convert.ToInt32(convertToInt);

when str is a null reference it will throw ArgumentNullException.
Exe.
// It will raise Argument Null Exception
parseResult= int.Parse(nullString);

when str is a null reference it will return 0 rather than throw ArgumentNullException.
Exe.
//It will ouput as 0 if Null string is there
parseResult= Convert.ToInt32(nullString);

if str is other than integer value it will throw FormatException.
Exe.
//It will raise Format Exception
parseResult= int.Parse(formatException);

if str is other than integer value it will throw FormatException.
Exe.
//It will raise Format Exception
parseResult= Convert.ToInt32(formatException);

when str represents a number less than MinValue or greater than MaxValue it will throw OverflowException.
Exe.
//It willl raise Over Flow Exception
parseResult= int.Parse(maxValue);

when str represents a number less than MinValue or greater than MaxValue it will throw OverflowException.
Exe.
//It will raise Over Flow Exception
parseResult= Convert.ToInt32(maxValue);


No comments:

Post a Comment