Zoo tutorials: [ SQL | Linux | XML ]
ProgZoo: [ Java | C# | VB | C++ | Perl ]
Log in

A Gentle Introduction to
C# Programming

Converting a String to a number

 

Data types

Converting a String to a number

1. [ C# ] String to int


Big

The static method int.Parse will an integer from a string

There is a danger that the string does not represent a valid number in which case an exception will be raised.

The class Convert may also be used for parsing strings.

Other Parse methods are available with the other base types: float.Parse long.Parse double.Parse bool.Parse

Other Convert methods include Convert.ToInt32 Convert.ToInt64 Convert.ToBoolean Convert.ToSingle Convert.ToDouble

2. [ C# ] Catch the exception


Big

If the string is not a valid integer then an exception is raised.