Note that there are some explanatory texts on larger screens.

plurals
  1. POBoxing Occurrence in C#
    primarykey
    data
    text
    <p>I'm trying to collect all of the situations in which boxing occurs in C#:</p> <ul> <li><p>Converting value type to <code>System.Object</code> type:</p> <pre><code>struct S { } object box = new S(); </code></pre></li> <li><p>Converting value type to <code>System.ValueType</code> type:</p> <pre><code>struct S { } System.ValueType box = new S(); </code></pre></li> <li><p>Converting value of enumeration type to <code>System.Enum</code> type:</p> <pre><code>enum E { A } System.Enum box = E.A; </code></pre></li> <li><p>Converting value type into interface reference:</p> <pre><code>interface I { } struct S : I { } I box = new S(); </code></pre></li> <li><p>Using value types in C# string concatenation:</p> <pre><code>char c = F(); string s1 = "char value will box" + c; </code></pre> <p><strong>note:</strong> constants of <code>char</code> type are concatenated at compile time</p> <p><strong>note:</strong> since version 6.0 C# compiler <a href="https://github.com/dotnet/roslyn/pull/415" rel="nofollow noreferrer">optimizes concatenation</a> involving <code>bool</code>, <code>char</code>, <code>IntPtr</code>, <code>UIntPtr</code> types</p></li> <li><p>Creating delegate from value type instance method:</p> <pre><code>struct S { public void M() {} } Action box = new S().M; </code></pre></li> <li><p>Calling non-overridden virtual methods on value types:</p> <pre><code>enum E { A } E.A.GetHashCode(); </code></pre></li> <li><p>Using C# 7.0 constant patterns under <code>is</code> expression:</p> <pre><code>int x = …; if (x is 42) { … } // boxes both 'x' and '42'! </code></pre></li> <li><p>Boxing in C# tuple types conversions:</p> <pre><code>(int, byte) _tuple; public (object, object) M() { return _tuple; // 2x boxing } </code></pre></li> <li><p>Optional parameters of <code>object</code> type with value type default values:</p> <pre><code>void M([Optional, DefaultParameterValue(42)] object o); M(); // boxing at call-site </code></pre></li> <li><p>Checking value of unconstrained generic type for <code>null</code>:</p> <pre><code>bool M&lt;T&gt;(T t) =&gt; t != null; string M&lt;T&gt;(T t) =&gt; t?.ToString(); // ?. checks for null M(42); </code></pre> <p><strong>note:</strong> this may be optimized by JIT in some .NET runtimes</p></li> <li><p>Type testing value of unconstrained or <code>struct</code> generic type with <code>is</code>/<code>as</code> operators:</p> <pre><code>bool M&lt;T&gt;(T t) =&gt; t is int; int? M&lt;T&gt;(T t) =&gt; t as int?; IEquatable&lt;T&gt; M&lt;T&gt;(T t) =&gt; t as IEquatable&lt;T&gt;; M(42); </code></pre> <p><strong>note:</strong> this may be optimized by JIT in some .NET runtimes</p></li> </ul> <p>Are there any more situations of boxing, maybe hidden, that you know of?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload