Boxing and unboxing: when does it come up?

前端 未结 7 865
灰色年华
灰色年华 2020-12-01 11:22

So I understand what boxing and unboxing is. When\'s it come up in real-world code, or in what examples is it an issue? I can\'t imagine doing something like this example:

相关标签:
7条回答
  • 2020-12-01 11:34

    Boxing/unboxing occurs when a value type (like a struct, int, long) is passed somewhere that accepts a reference type - such as object.

    This occurs when you explicitly create a method that takes parameters of type object that will be passed value types. It also comes up when you use the older non-generic collections to store value types (typically primitives).

    You will also see boxing occuring when you use String.Format() and pass primitives to it. This is because String.Format() accepts a params object[] - which results in boxing of the additional parameters in the call.

    Using reflection to invoke methods can also result in boxing/unboxing, because the reflection APIs have no choice but to return object since the real type is not known at compile time (and the Reflection APIs cannot be generic).

    The newer generic collections do not result in boxing/unboxing, and so are preferable to the older collections for this reason (eg ArrayList, Hashtable, etc). Not to mention they are typesafe.

    You can avoid boxing concerns by changing methods that accept objects to be generic. For example:

    public void string Decorate( object a ) // passing a value type results in boxing
    {
       return a.ToString() + " Some other value";
    }
    

    vs:

    public void string Decorate<T>( T a )
    {
       return a.ToString() + " some other value";
    }
    
    0 讨论(0)
  • 2020-12-01 11:39

    Boxing and unboxing is really moving from value type to reference type. So, think of it as moving from the stack to the heap and back again.

    There certainly are cases where this is relevant. The inclusion of generics in the 2.0 framework cut a lot of common boxing cases out of practice.

    0 讨论(0)
  • 2020-12-01 11:41

    Since the advent of strongly-typed lists and dictionaries using generics with C# 2.0 (Visual Studio 2005), I think the importance of keeping boxing/unboxing in mind have been amazingly minimized. Add to that nullable types (int?, etc.) and using the null coalescing operator (??) and it really shouldn't be much of a concern at all and would likely not see it in any code that's not 1.1 Framework or earlier.

    0 讨论(0)
  • 2020-12-01 11:42

    Boxing (in my experience) usually occurs in these cases:

    • A value type is passed to a method that accepts an argument of type Object.
    • A value type is added to a non-generic collection (like an ArrayList).

    Other times you can see boxing and unboxing is when you use reflection as the .NET framework's reflection API makes heavy use of Object.

    0 讨论(0)
  • 2020-12-01 11:42

    Here is a really nasty one :)

    SqlCommand cmd = <a command that returns a scalar value stored as int>;
    
    // This code works very well.
    int result = (int)cmd.ExecuteScalar();
    
    // This code will throw an exception.
    uint result = (uint)cmd.ExecuteScalar();
    

    The second execute fails because it tries to unbox an Int32 into an UInt32 which is not possible. So you have to unbox first and than cast.

    uint result = (uint)(int)cmd.ExecuteScalar();
    
    0 讨论(0)
  • 2020-12-01 11:49

    It's much less of an issue now than it was prior to generics. Now, for example, we can use:

    List<int> x = new List<int>();
    x.Add(10);
    int y = x[0];
    

    No boxing or unboxing required at all.

    Previously, we'd have had:

    ArrayList x = new ArrayList();
    x.Add(10); // Boxing
    int y = (int) x[0]; // Unboxing
    

    That was my most common experience of boxing and unboxing, at least.

    Without generics getting involved, I think I'd probably say that reflection is the most common cause of boxing in the projects I've worked on. The reflection APIs always use "object" for things like the return value for a method - because they have no other way of knowing what to use.

    Another cause which could catch you out if you're not aware of it is if you use a value type which implements an interface, and pass that value to another method which has the interface type as its parameter. Again, generics make this less of a problem, but it can be a nasty surprise if you're not aware of it.

    0 讨论(0)
提交回复
热议问题