Type Conversions in C#C# is a strongly typed language, and that makes mixing data stored in variables of different data types a process that requires thought. For example, look at this code: int source = 5; long target; target = source; System.Console.WriteLine(target); Here, we're creating an int variable, source , and a long variable, target , and assigning the value in source to target . Although these variables are not of the same data type, no data will be lost, because this is a widening conversion , where we're moving from a smaller container ( int ) to a larger one ( long ). Because data is not lost in a widening conversion, C# has no problem making this conversion, and will do so automaticallythis is called implicit conversion. On the other hand, look at this code, where we've switched the data types of the source and target variables: long source = 5; int target; target = source; System.Console.WriteLine(target); Now it looks to C# as if we're trying to cram a long into an int , and that implies a possible loss of data accuracy. This is a narrowing conversion , and you'll see an error message like this when you try to compile: conversion.cs(7,18): error CS0029: Cannot implicitly convert type 'long' to 'int' On the other hand, we happen to know that the long only contains 5 , which means you can assign it to an int without loss of accuracy, so we can use an explicit cast , (int) , to let the compiler know that we really do want to read the value in source , convert that value to an int , and assign that value to target , as you see in ch01_07.cs, in Listing 1.7. Listing 1.7 Using an Explicit Type Case (ch01_07.cs) class ch01_07 { static void Main() { long source = 5; int target; target = (int) source; System.Console.WriteLine(target); } } Now the code compiles and runs without problem. Explicit casts, which you create with ( type ) , where type is the type you're casting to, can be made between many different types in C#. Table 1.6 shows the possibilities for numeric conversions. Table 1.6. Allowed Explicit Type Conversions for C# Numeric Types
What if you're not working with a numeric type? What if you want to convert a string to a number, for example? It's clear that there's more going on here than an explicit cast can handle. In this case, C# supports a Parse method for each numeric type, enabling you to convert strings to numbers . For example, int.Parse parses strings and returns an int , float.Parse does the same for float values, and so on. You can see an example in ch01_08.cs, Listing 1.8, where we're reading in an integer as a string, parsing it, and displaying it. Listing 1.8 Parsing Integers from Strings (ch01_08.cs) class ch01_08 { static void Main() { System.Console.Write("Please enter an integer: "); int value = int.Parse(System.Console.ReadLine()); System.Console.WriteLine("You entered: {0}", value); } } Here's what you might see when you run ch01_08.cs: C:>ch01_08 Please enter an integer: 5 You entered: 5 How about going the other way and converting a number to a string? Every object in C# supports the ToString method, and you can always call that method to get a string representation of that object. Here's all you need to do to convert an integer to a string: int intNumber = 5; string text = intNumber.ToString(); The .NET Framework also provides the heavy-duty Convert class, which can convert most types to other types. For example, here's how you convert a string to a C# int type (which is an Int32 type as far as the .NET Framework is concerned ): int value = Convert.ToInt32(Console.ReadLine()); Unlike the Parse method, which only handles strings, the Convert class methods can accept all kinds of datafor example, you can pass a string to Convert.ToInt32 , or a long , a float , and so on. Here are the conversion methods of the Convert class:
|