Both function use for string format to integer value.
There are some cases
1) If string is null Parse throws ArugmentNullException
2) If String it not compatible to int it throws Format Exception
3) If value is less than Minimum value or greater than Maximum value it will throws Overflow Exception
While in case of Contvert.ToInt32. This function is a wrapper of Int32.Parse. It throws the same exception except in case of null string it returns zero(0).
Example
string s1 = "20";
string s2 = "25.50";
string s3 = null;
string s4 = "123456789123456789123456789123456789123456789";
int result;
bool success;
result = Int32.Parse(s1); //-- 1234
result = Int32.Parse(s2); //-- FormatException
result = Int32.Parse(s3); //-- ArgumentNullException
result = Int32.Parse(s4); //-- OverflowException
result = Convert.ToInt32(s1); //-- 25
result = Convert.ToInt32(s2); //-- FormatException
result = Convert.ToInt32(s3); //-- 0
result = Convert.ToInt32(s4); //-- OverflowException
Int32.TryParse doesn't throws any exception instead it returns zero if value is not compatible. and return false.
success = Int32.TryParse(s1, out result); //-- success => true; result => 25
success = Int32.TryParse(s2, out result); //-- success => false; result => 0
success = Int32.TryParse(s3, out result); //-- success => false; result => 0
success = Int32.TryParse(s4, out result); //-- success => false; result => 0
Friday, September 16, 2011
Subscribe to:
Post Comments (Atom)
Followers
Link
Labels
Agille
(3)
Angular 2
(96)
Architecture
(7)
ASP .Net
(30)
C#/VB .Net
(161)
Entityframework
(11)
General
(33)
Git
(2)
Hosting
(2)
HTML
(4)
Interview
(12)
Interview-Agile
(1)
Interview-Angular
(25)
Interview-ASP .Net
(3)
Interview-C#
(16)
Interview-JavaScript
(1)
Interview-MVC
(30)
Interview-SQL Server
(4)
Interview-WCF
(2)
Islam
(6)
Java Script
(37)
JQuery
(1)
MVC
(53)
NodeJS
(1)
Puzzles
(1)
React-Native
(3)
Security
(1)
Shayri
(10)
SQL Server
(41)
VC++
(5)
WCF
(21)
Web API
(27)
WPF
(21)
No comments:
Post a Comment