Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 1
    Would you mind explaining why you used "boxing" and not "casting"? What boxing happens here? Are objects allocated/deallocated? Commented Nov 7, 2013 at 19:54
  • 3
    @BenjaminGruenbaum You are correct that casting would be more general. That said, the real difference at runtime is when you're dealing with value types (which is what I assumed when I wrote "boxing"). For reference types, the behavior is effectively the same as ArrayList at runtime. Statically though, it'll require a cast with ArrayList. Commented Nov 8, 2013 at 8:40
  • I was wondering whether framework should restrict T to be of "object" type, since ArrayList implicitly allows that. Commented Sep 16, 2017 at 13:38
  • I'd like to (belatedly) add to @ScottAdams point: that blog talks about issues with Java 5's implementation of generics, which is different enough from .NET's implementation that it's just not relevant to this question. Neither of the "harmful" examples mentioned in the post are problematic in .NET, so if you want to talk about the "gotchas" of .NET generics, you'll want to use a different source. Commented Jul 16, 2020 at 3:31
  • boxing and unboxing refers to casting (boxing is implicit and unboxing is explicit). When a value is boxed, it gets stored on the heap and uses more memory. It requires more time to unbox a value. Generally boxing/unboxing should be avoid if possible. you can read more here: learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/… Commented Sep 10, 2021 at 22:14