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 { ...

Get .NET Common Language Runtime Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.