O'Reilly logo

.NET Common Language Runtime Unleashed by Kevin Burton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

System.Convert

This class converts one base type to another base type. All of the basic types are represented in each version, but that conversion might not actually occur. Three different cases exist:

  • No conversion takes place. An example of this is converting a type to itself.

  • If the conversion does not make sense, the Convert class method throws an exception (InvalidCastException, FormatException, OverflowException, and so on).

  • The Convert method succeeds in making the conversion.

Listing B.4 shows an example of a conversion from a string read in the Console to a DateTime structure (convert.cs).

Listing B.4. Conversion Example
 public static void Main(String[] args) { Console.Write("Input a date: "); string input = Console.ReadLine(); try { ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required