Boxing and unboxing: when does it come up?

前端 未结 7 914
灰色年华
灰色年华 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: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.

提交回复
热议问题