Avoid Boxing and Unboxing to improve performance

While writing code we generally don’t pay much attention to boxing & unboxing. It does matter in performance. And a highly skilled and experienced C# / .NET developer must pay attention to everything that can improve performance.

Here is an excerpt from MSDN article:

In relation to simple assignments, boxing and unboxing are computationally expensive processes. When a value type is boxed, a new object must be allocated and constructed. To a lesser degree, the cast required for unboxing is also expensive computationally.

Side effects of boxing and unboxing:

  1. Boxed value type objects take up more memory
  2. Boxed value type objects require an additional read
  3. Short-lived value type objects eat up Gen 0 heap & this forces frequent garbage collections
  4. Boxing and unboxing operations consume CPU & time
  5. Casting is required & it can be costly

How to prevent boxing & unboxing:

  1. Use ToString method of numeric data types such as int, double, float etc.
  2. Use for loop to enumerate on value type arrays or lists (do not use foreach loop or LINQ queries)
  3. Use for loop to enumerate on characters of string (do not use foreach loop or LINQ queries)
  4. If you define your own value type then override implementation of basic object methods.
  5. Don’t assign value type instance to object unless unavoidable
  6. Use generic List<>, Dictionary<> (et al) instead of ArrList & HashTable.
  7. Use Nullable<> value types (examples int?, float? etc)
  8. When using string.Format (SrtingBuilder.AppendFormat) or similar API’s that use ‘params object[]’ pass on value type objects by calling ‘ToString()’ method

Avoid Boxing and Unboxing to improve performance

Pay attention for below mentioned things & do some code refactoring

  1. Implicit boxing (Example: object num = 1; )
  2. Use of foreach on value types
  3. LINQ queries on value type collections
  4. Casting to value types

Here is my another post about Effectively use StringBuilder to improve performance while concatenating strings which is related to similar performance topic.

Boxing and Unboxing of value types