title: VB.NET Boxing weirdness
body: <p>I can't understand what is happening with the following code in VB.NET.  When I run this code:</p>
  
  <pre><code>Public Function test() As Boolean
      Dim  a As Integer = 1
      Dim b As Object = a
      Dim c As Object = b
      Return Object.ReferenceEquals(b, c)
  End Function
  </code></pre>
  
  <p>Then the function returns <code>True</code>.  However, if I run this:</p>
  
  <pre><code>Structure TTest
      Dim i As Integer
      Dim tid As Integer
      Sub New(ByVal _i As Integer, ByVal _tid As Integer)
          i = _i
          tid = _tid
      End Sub
  End Structure
  
  Public Function test_2() As Boolean
      Dim a As New TTest(1, 1)
      Dim b As Object = a
      Dim c As Object = b
      Return Object.ReferenceEquals(b, c)
  End Function
  </code></pre>
  
  <p>Then it returns <code>False</code>.  In both functions, I declare two value type variables, an <code>Integer</code> on the first and a custom <code>Structure</code> on the second one.  Both should be boxed upon object assignment, but in the second example, it seems to get boxed into two different objects, so <code>Object.ReferenceEquals</code> returns <code>False</code>.</p>
  
  <p>Why does it work this way? </p>