O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

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

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.

Figure 9.4 is very useful in determining whether ...

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