Does casting create new object?

后端 未结 4 778
别那么骄傲
别那么骄傲 2020-12-10 11:31

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

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-10 12:00

    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).

提交回复
热议问题