class ConvertIt
{
static void Main()
{
string stringToNumber = "1000";
int numberParse = 0;
int numberConvertTo = 0;
int numberTryParse = 0;
bool isTryParse = false;
// Using .Parse() will result in throwing ArgumentNullException in case of string being null.
numberParse = int.Parse(stringToNumber);
// Using Convert.ToInt32() will result in 0 in the case of string being null.
numberConvertTo = System.Convert.ToInt32(stringToNumber);
// Better than .Parse() as we get to know whether the string got converted or not Without throwing ArgumentNullException
isTryParse = int.TryParse(stringToNumber, out numberTryParse);
if(isTryParse == true)
{
System.Console.WriteLine("Conversion to integer successful");
}
else
{
System.Console.WriteLine("Conversion to integer failed");
}
System.Console.WriteLine("numberParse: "+numberParse);
System.Console.WriteLine("numberConvertTo: "+numberConvertTo);
System.Console.WriteLine("numberTryParse: "+numberTryParse);
}
}
About Me
- Dark
- B.E.(Computer Science), Android/Java Developer, CCNA, CCNA SECURITY (IINS), CCNP (R&S), 4011 Recognized(NSA & CNSS)U.S.A. , MCSA, MCTS, REDHAT CERTIFIED NETWORK SECURITY ADMINISTRATOR(RH253), AFCEH.
Sunday, December 1, 2013
Conversion of String to Integer in C#
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment