21

I am quite unsure here:

Image i=some image...

Bitmap B=(Bitmap)i;

The B now points to the same object as i. I am confused...I would say that Bitmap B will point to new instance of Image that is casted to bitmap but it is obviously not the case. Then I just do not get how it works here.

1
  • Image and Bitmap are .NET classes, no need to post definitions. Image is an interface that is implemented by Bitmap. Feb 21, 2011 at 10:04

3 Answers 3

40

Casting does not create a new object (at least, not unless new conversion operators have been defined, which is uncommon in non-numeric types, and doesn't apply in your example). It merely instructs the compiler how to "treat" an object. In the case you present, you're telling the compiler "don't worry, trust me, B is actually a Bitmap". If it turns out you've told it a fib, the runtime will catch you on it by throwing an InvalidCastException at runtime.

MSDN has some more information.

A cast is a way of explicitly informing the compiler that you intend to make the conversion and that you are aware that data loss might occur

4
  • 2
    Actually, it's possible that casting creates a new object. Think for example to a class that overrides explicit operator returning a new object. But, of course, the OP example is not the case...
    – digEmAll
    Feb 21, 2011 at 10:15
  • Also, that b will point still to the same object but from "different point of view"?
    – FlyBoy
    Feb 21, 2011 at 10:21
  • @Michael Petrotta: Thanks. What confuses me a bit is "possible data loss". In this case, there still will be variable i of type Image so there is no real data loss. Just the new type might not offer all properties / methods as the previous one
    – FlyBoy
    Feb 21, 2011 at 10:25
  • @FlyBoy: yeah, that verbiage applies more to numeric casts, like short s = (short)someInteger;. If the absolute value of someInteger is less than 2^15, then you won't see data loss; greater than, and you will. Feb 21, 2011 at 10:28
3

A Dog is a specialised form of Animal. Dogs have dog-specific properties and behaviour (bark, lickPrivateParts) but also have the properties and behaviour common to all members of the group Animal (numberOfChromosomes, breathe, eat etc.).

If you cast a Dog to Animal you are upcasting (treating a more specialised class as a less specialised base class). While cast to Animal the compiler/runtime will 'see' the Dog as a basic Animal and dog-specific properties and behaviour will not be available for this upcast dog-animal. This makes sense since, for example, a generic Animal will not 'bark'.

You are not creating a new Animal instance when you do this, rather you are using the Dog as if it was a less specialised Animal object.

Similarly, if you cast a Bitmap to an Image you will (for the duration of the time you're treating your Bitmap as an image) only be able to access the fields/properties of Image, not Bitmap.

One point to mention is that what you are doing in your example is downcasting (going from a less specialised to more specialised object). This is not always safe or sensible - if you think about it an instance of the Animal class doesn't have values or definition for the Dog-specific attributes.

0

When you construct a new object using new Obj(), a new object is created. When you cast that object to another object type, the object itself remains unchanged, the runtime will just treat it differently (work with the object as being of a different type).

You can cast a List to IEnumerable and work with the same List object as being of type IEnumerable; the underlying list still exists and is unchanged.

This only works if at least one of the following is true:

  • The object you want to cast to is a base class of the object to be casted.
  • The object to be casted implements the interface to be cast to.
  • The object provides a specific cast to the resulting type (using explicit operator).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.