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.9. Casting for reference types

Like Java, both C#'s variables and objects are typed. [7] Assuming that there are two classes Vehicle and Car, and Vehicle is the superclass of Car, you can have an object of class Car being referred to by a variable of type Vehicle. And, like Java, you cannot have a Vehicle object (object of a super type) referenced by a variable of type Car (variable of subtype). Similarly, you can explicitly cast an object to a superclass type.

[7] This is not the case for some OO programming languages. In Ruby, for example, variables have no types. A variable can refer to any object of any class.

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