-1

One way to avoid boxing in C# is to pass the value type by reference. I have read that a generic method can also be used to avoid boxing. Although writing a generic method solely for the purpose of avoiding boxing seems to be a little extreme - if the type will always be the same.

My question is - if writing code for the best performance and to avoid boxing, is it reasonable to pass all value types (like an int) by reference - even though the method in question is only working on the object and not creating it? Are there any drawbacks to this?

  • 6
    *if the type will always be the same* why would you declare method to take `object` (which would result in boxing operation)? Just declare it to take the value type you expect and there will be no boxing. – MarcinJuraszek Sep 17 '14 at 23:59
  • If "type will always be the same" where boxing will come from? If your method takes that particular type there is no boxing... Sample code likely would clarify it. – Alexei Levenkov Sep 18 '14 at 00:00
  • 2
    "pass all value types (like an int) by reference" --- why do you think it should improve performance? – zerkms Sep 18 '14 at 00:02
  • 1
    check this link http://stackoverflow.com/questions/3395873/pass-by-value-vs-pass-by-reference-performance-c-net and put some code sample what you try to achieve and then we can give some suggestions. – Seminda Sep 18 '14 at 00:21
  • 2
    It is pretty pointless to assume anything when you can look at the generated code. Having idlasm.exe handy is important, you can always see the BOX instruction. And no, this doesn't happen and passing a value by reference is very inefficient. Never help until you've *measured* that you need to help. Then you look. – Hans Passant Sep 18 '14 at 00:23
  • Besides, passing an `int` by reference will either be just as "expensive" on a 32-bit system.. or twice as "expensive" on a 64-bit system. – Simon Whitehead Sep 18 '14 at 00:35
  • Mia culpa - I had thought that any time a value type is passed, it gets boxed, regardless of whether the parameter it is going to is an object or the same type as specified in the called method. My bad... I have asked for moderation for the question to be deleted. –  Sep 18 '14 at 06:14

1 Answers1

8

The best way to avoid boxing of value types is: just use them as values!

I think you have completely misread that reference. What it says is that using ref parameters does not cause boxing. It does not say that it is a way to avoid boxing.

Boxing happens when a value type is used in a reference context, such as being cast to an Object. This article says that passing parameters by reference must not be confused with the concept of reference types, but confuse them seems to be exactly what you've done.

Summary by 280Z28:

In other words, avoid the following two operations:

  1. Casting or assigning the value to a variable of type object (or passing the value as an argument for a method parameter of type object).
  2. Casting or assigning the value to a variable which is an interface type (such as IEnumerable), or passing the value as an argument for a method parameter which is an interface type.

There are exceptions to this rule (e.g. calling some generic methods), and there are cases where boxing can occur in other contexts, but these are the primary situations to be aware of when you are trying to avoid unnecessary boxing of value types.

Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
david.pfx
  • 10,520
  • 3
  • 30
  • 63