If I have an object such as List<string> that I cast into an object, and then back again, will all the strings get cast as well or just the list that contains them?
I’m thinking that the compiler would only have to check if the object was of type List<string> before casting back into a List<string> but I grew up in C#, so I’m not entirely certain what goes on behind the code that I write.
When you cast a
List<string>to anobject, you’re not really doing any casting at all. You’re assigning one reference to some data, to a less-specific reference. Thestringobjects that it contains aren’t changed at all, either.Also, to clarify, there is no boxing involved in this case. Boxing occurs when you create a reference to a value type like an
intor somestruct, by assigning it or passing it somehow to a variable of typeobject.