Int.Parse() :-
Int.Parse(string s) method converts the
string to Integer. If string s is null then it will throw
ArgumentNullException. If string s is other than integer value, then it will
throw FormatException. If string s is out of rang integer, then it will throw
OverFlowException.
Example:-
class Program
{
static void Main(string[] args)
{
string val = "1000";
string val1 = null;
string val2 = "100.34";
string val3 = "999999999999999999999";
int res;
res = int.Parse(val); // sucess
res = int.Parse(val1); //
ArgumentNullException
res = int.Parse(val2); //
FormatException
res = int.Parse(val3);
// OverFlowException
Console.ReadKey();
}
}
int.TryParse :-
Int.TryParse(string s, out int result) method converts the string to Integer out variable and return true
if successfully parsed. If string s is null then it will return 0. If string s
is other integer value then out variable return 0 rather than FormatException.
If string s is out of rang integer, then it will throw OverFlowException.
Example:-
class Program
{
static void Main(string[] args)
{
string val = "1000";
string val1 = null;
string val2 = "100.34";
string val3 = "999999999999999999999";
bool finalRes;
int res;
finalRes = int.TryParse(val, out
res); // sucess
finalRes = int.TryParse(val1,out
res); // 0
finalRes = int.TryParse(val2,out
res); // 0
finalRes = int.TryParse(val3,out
res); // 0
Console.ReadKey();
}
}
Convert.ToInt32():-
Int.Parse(string s) method
converts the string to Integer. If string s is null then it will return 0. If
string s is other than integer value, then it will throw FormatException. If
string s is out of rang integer, then it will throw OverFlowException.
Example:-
class Program
{
static void Main(string[] args)
{
string val = "1000";
string val1 = null;
string val2 = "100.34";
string val3 = "999999999999999999999";
int res;
res = Convert.ToInt32(val); //
sucess
res = Convert.ToInt32(val1); //
0
res = Convert.ToInt32(val2); //
FormatException
res = Convert.ToInt32(val3); //
OverFlowException
Console.ReadKey();
}
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.