3

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.

Community
  • 1
  • 1
Tom Wright
  • 11,278
  • 15
  • 74
  • 148
  • 2
    If you do this: `object x = (object) 24;`, you've boxed. – rory.ap May 11 '17 at 11:09
  • 1
    Nothing has changed - conditions are the same – Sergey Berezovskiy May 11 '17 at 11:09
  • Please see my edit - I'm not asking how to box, or whether it's still possible. I'm asking why someone would box these days. – Tom Wright May 11 '17 at 11:12
  • 2
    `ArrayList` 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
  • @Sinatr Not a dupe. The whole premise of my question is that I understand the difference between ArrayList and List. – Tom Wright May 11 '17 at 11:14
  • 1
    @TomWright -- That's totally different from the way your question was framed. Your title says "when" not "why". – rory.ap May 11 '17 at 11:14
  • Then perhaps [object vs generics](http://stackoverflow.com/q/4424030/1997232). – Sinatr May 11 '17 at 11:14
  • 1
    interfacing. especially interfacing with annoyingly outdated third-party components. – Cee McSharpface May 11 '17 at 11:15
  • 1
    The proof that boxing is still needed, is to find the first example where you can't solve the problem by another other (simple) means than through boxing. I haven't used boxing for the last ten years, and I've solved a lot of complicated stuff without needing it. – Frode May 11 '17 at 11:16
  • But...maybe some of the mechanisms I actually use uses boxing under the hood. Not sure there – Frode May 11 '17 at 11:18
  • 1
    The last time I needed it (outside of the framework): Walking recursivly through object properties with specific attributes. Rule of thumb: Always prefer generics, but sometimes it is not possible to do some task only with generics. Especialy if I think about DRY, I often end up using Reflection and Boxing. – Christian Gollhardt May 11 '17 at 11:41
  • You can box/unbox even with generics, using a `List`... – Zev Spitz May 11 '17 at 12:04

2 Answers2

3

Boxing and unboxing is not something that you explicitly do; it is something that happens all the time whenever you have a struct in your hands and you are passing it to some receiver that expects an object. So, consider this code:

public void SafeToString( Object a )
{
    if( a != null )
        return a.ToString();
    return "null";
}

SafeToString( 42 );

If you had said 42.ToString() there would be no boxing, because 42 is known by the compiler to have a ToString() method, and struct cannot be subclassed, so the ToString() method that operates on 42 is known at compilation time.

However, when you say SafeToString( 42 ); the 42 gets boxed into an object, which is passed to SafeToString(), which invokes Object.ToString(), which resolves to the boxing object's ToString(), which delegates to int.ToString().

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • Hi Mike. Thanks for this answer. Hope you don't mind, but I've quoted it in a blog I've just pubished: http://blog.tdwright.co.uk/2017/05/12/what-is-boxing-and-why-should-you-care/ – Tom Wright May 12 '17 at 18:40
2

Generics is a good thing, but they aren't capable completely remove the necessity of dealing with boxing.

ArrayList is obsolete, right, but sometimes you still going to use List<object> when storing different types in one list (when only Object is something they have in common).

Another example generic methods. Again, they are good if you know type at compile-time, but what if you want something to work with any type? Good old object parameter and boxing (with casting) to the rescue.

Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • Thanks for this answer. I hope you don't mind, but I referenced this in a blog post I've just published: http://blog.tdwright.co.uk/2017/05/12/what-is-boxing-and-why-should-you-care/ – Tom Wright May 12 '17 at 18:39