9.10 Casting for value types


Like Java, casting in C# can be implicit as well as explicit. Widening casts [8] can be performed implicitly, while narrowing casts have to be stated explicitly.

[8] A widening cast is a cast from a simple type of lower range to one of higher range. Examples include casting a short to an int , an int to a long , a float to a double . A narrowing cast is the opposite . Casting a long to an int is a narrowing cast.

Figure 9.4 shows the allowed implicit casts in C#. Unlike Java, which has only eight primitive types, the C# picture is more complex with four more types.

Figure 9.4. Direction of arrows shows legal implicit casting. The shaded boxes are integral simple types.

graphics/09fig04.gif

Figure 9.4 is very useful in determining whether you are performing a narrowing or widening conversion. A type at the start of the arrow can be implicitly cast to a type at the end of the arrow without any loss of precision or overflow. The diagram can be read recursively “ for example, a byte can be cast into a decimal because a forward path exists from byte to decimal . Alternatively, a uint can be implicitly cast into a long , float , double , ulong , or decimal .

Explicit casting is done in the same way as in Java. Examine the program following.

 1: using System;  2:  3: public class Test{  4:  5:   public static void Main(string []args){  6:     double d = 1.11111111;  7:  float f  =  (float)d;  8:  ulong ul  =  (ulong)f;  9:     System.Console.WriteLine(d); 10:     System.Console.WriteLine(f); 11:     System.Console.WriteLine(ul); 12:   } 13: } 

Output:

 c:\expt>test 1.11111111 1.111111 1 

Line 7 in the program above performs an explicit cast of a double to a float . Line 8 shows an explicit cast from a float to a ulong . Both are narrowing casts.

Note that although decimal is also used to store floating point values, the decimal type is structured differently from a float or double , and explicit casting is required between decimal and a float or a double in either direction.

Remember that (like Java) an explicit narrowing cast may result in some unexpected results because of overflow or underflow. The code below shows some explicit casts which result in meaningless values because the narrowing cast results in an underflow.

 1: using System;  2:  3: public class Test{  4:  5:   public static void Main(string []args){  6:     int i = -32769;  7:  short s  =  (short)i;  // range of s is -32768 to 32767  8:  uint ui  =  (uint)i;  // range of ui is 0 to 4294967295  9:     System.Console.WriteLine(i); 10:     System.Console.WriteLine(s); 11:     System.Console.WriteLine(ui); 12:   } 13: } 

Output:

 c:\expt>test -32769 32767 4294934527 

C# has a special checked operator (which has no equivalent in Java) that can be used to throw an exception if an explicit cast causes an overflow (see section 10.4).



From Java to C#. A Developers Guide
From Java to C#: A Developers Guide
ISBN: 0321136225
EAN: 2147483647
Year: 2003
Pages: 221
Authors: Heng Ngee Mok

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net