This article is just quick recap of majority of cases in which boxing occurs in C#6.0 or higher.

Boxing occurs everywhere when we need a reference type to a value type. Here is the most popular example:

int number = 123;  
object boxed = number;

But what about another cases that are not so obvious? Let’s consider them.

public enum Color
    RED = 1,
    GREEN = 2,
    BLACK = 3

public interface IPrintable
    void Print();

public struct Number : IPrintable
    public int Value { get; set; }

    public void Print()
        Console.WriteLine("Value = {0}"this.Value);

    public override string ToString()
         return base.ToString();

#1 Converting value type to System.Object type:

object number = new Number();

#2 Converting value type to System.ValueType type:

System.ValueType number = new Number();

#3 Converting value of enumeration type to System.Enum type:

System.Enum color = Color.GREEN;

#4 Converting value type into interface reference:

IPrintable number = new Number();

#5 Using value types in C# string concatenation:

Action print = new Number().Print;

#6 Calling Object.GetType method on value types:

new Number().GetType();

#7 Calling a base class method from a struct:

var number = new Number();
var str = number.ToString();

#8 Constant patterns under is expression:

int value = 1;
if (value is 1) { //both ‘value’ and ‘1’ will be boxed

Memorizing the examples above doesn’t guarantee you will never get undesirable boxing. It’s important to be quite attentive to every piece of code you write. Also it would be great to check the code in Ildasm or dotPeek tools.