I'm trying to explain boxing to a junior colleague.
The canonical example seems to be ArrayList
. For example:
But this has been superseded by List<T>
from C# 2, when generics were introduced (as explained in this answer).
So, in the age of generics, under what circumstances would I find myself boxing values?
Edit: To be clear, I'm not asking whether it is still possible to use boxing. I'm asking why we would use boxing now that generics have made ArrayList
obsolete.
Edit 2: I thought this was already clear, but I'm also not asking about the difference between an ArrayList
and a List<T>
. In fact, this question is entirely premised on the fact that I appreciate that generics mean we don't have to use an ArrayList
, and we therefore don't need to box values in these circumstances.
object x = (object) 24;
, you've boxed. – rory.ap May 11 '17 at 11:09ArrayList
is not the only class that takes object,DataTable
and many others too. That's only .NET, there are many public interfaces that take or return objects or your own code might do too. – Tim Schmelter May 11 '17 at 11:14