Can someone please explain why this doesn’t work?
MyClass myClass1 = new MyClass();
object obj = myClass1;
MyClass myClass2 = obj; <-- error
If obj “points” to the same block of memory of type MyClass, then why can I not “point” myClass2 to the same block of memory on the last line?
Thanks for any help.
The type of myClass2 is “
MyClass“. You can assign to it any value that is of a type that is, or derives from,MyClass.objectis not and does not derive fromMyClass, so you need a cast.If it were able to do this implicitly, what would happen if you did it with an object that is not really a
MyClass?