Witscale Test Center

10.5 Object reference casting


10.5 Object reference casting

Object reference casting uses the cast operator to change the type of object reference explicitly. We saw earlier various rules and situations where reference conversion take place. You can apply casting in all those situations. However, it is not required as conversion automatically changes the type. The real need for casting is to change the type that otherwise is not allowed in reference conversion. While you can forcefully change the type with casting, you cannot change any type into every other type. There are certain restrictions on the types that can be changed with casting. These restrictions are checked at two different times, the compile time and the runtime.