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 th
when you create a new object using new Obj(); a new object is created. When you cast that object to another object type, the object remains unchanged, only the runtime will work with the object as being of a different type.
You can cast a list to IEnumerable and work as of that point with the same list object as being of type IEnumerable.
This only works if - 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).
What you're doing doesn't look sensible! A Bitmap
is a subclass of Image
, so you can always take a Bitmap
object and refer to it as an Image
, but you can't guarantee that any particular Image
is a Bitmap
. If it isn't, you will end up with an exception being thrown.
You can create a new Bitmap
from an Image
, but you have to do that by creating a new instance, like
Image i = some image...
Bitmap b = new Bitmap(i);
That is not casting, that is creating a new Bitmap
object, but is a) legal and b) sensible.
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.
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