Is it possible to assign a base class object to a derived class reference with an explicit typecast?
问题 Is it possible to assign a base class object to a derived class reference with an explicit typecast in C#?. I have tried it and it creates a run-time error. 回答1: No. A reference to a derived class must actually refer to an instance of the derived class (or null). Otherwise how would you expect it to behave? For example: object o = new object(); string s = (string) o; int i = s.Length; // What can this sensibly do? If you want to be able to convert an instance of the base type to the derived