I'm not quite sure how conversions to and from System.Object (boxing/unboxing) fit into this picture.
Cheers, Thorsten
A little confused here to and get different results from C# & VB
Boxing/ Unboxing happens when converting from value type to reference type and vise-versa.
I think the more formal definition is something like
Boxing - The CLR creates a new heap-based object and copies the value from a value type to the new allocated object and then returns a reference to new object.
UnBoxing - The CLR copies a value from a reference type to a value type on the stack.
Something like that
I can not think of anyway else of doing this without the reference type being System.Object and how relates to System.ValueType
So anyone know why there is difference in the following examples where VB does not unbox int32?
C#using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BoxingCSharp
{
public class BoxCsharp
{
static void Main(string[] args)
{
int number = 5;
[color=red]object boxing = number;[/color]
object unboxObject = 5;
[color=blue]int unboxingInt = (int)unboxObject; [/color]
ValType vt;
[color=red]object oVtBox = vt;[/color]
[color=blue]ValType vtUnBox = (ValType)oVtBox;[/color]
}
}
struct ValType
{
}
class RefType : Object
{
}
}
IL from C#.method private hidebysig static void Main(string[] args) cil managed
{
// Code size 43 (0x2b)
.maxstack 1
.locals init ([0] int32 number,
[1] object boxing,
[2] object unboxObject,
[3] int32 unboxingInt,
[4] valuetype BoxingCSharp.ValType vt,
[5] object oVtBox,
[6] valuetype BoxingCSharp.ValType vtUnBox)
IL_0000: nop
IL_0001: ldc.i4.5
IL_0002: stloc.0
IL_0003: ldloc.0
[color=red] IL_0004: box [mscorlib]System.Int32[/color]
IL_0009: stloc.1
IL_000a: ldc.i4.5
IL_000b: box [mscorlib]System.Int32
IL_0010: stloc.2
IL_0011: ldloc.2
[color=blue] IL_0012: unbox.any [mscorlib]System.Int32[/color]
IL_0017: stloc.3
IL_0018: ldloc.s vt
[color=red] IL_001a: box BoxingCSharp.ValType[/color]
IL_001f: stloc.s oVtBox
IL_0021: ldloc.s oVtBox
[color=blue]IL_0023: unbox.any BoxingCSharp.ValType[/color]
IL_0028: stloc.s vtUnBox
IL_002a: ret
} // end of method BoxCsharp::Main
VB Namespace BoxingVB
Public Class BoxVB
Private Shared Sub Main(args As String())
Dim number As Int32 = 5
[color=red]Dim boxing As Object = number[/color]
Dim unboxObject As Object = 5
[color=green]'??????????????????????????????????[/color]
[color=blue]Dim unboxingInt As Int32 = unboxObject[/color]
[color=green] '??????????????????????????????????[/color]
Dim vt As ValType
[color=red]Dim oVtBox As Object = vt[/color]
[color=blue] Dim vtUnBox As ValType = oVtBox[/color]
End Sub
End Class
Structure ValType
End Structure
Class RefType
Inherits Object
End Class
End Namespace
IL From VB.method private static void Main(string[] args) cil managed
{
// Code size 57 (0x39)
.maxstack 2
.locals init ([0] object boxing,
[1] int32 number,
[2] object oVtBox,
[3] int32 unboxingInt,
[4] object unboxObject,
[5] valuetype BoxingVB.BoxingVB.ValType vt,
[6] valuetype BoxingVB.BoxingVB.ValType vtUnBox,
[7] valuetype BoxingVB.BoxingVB.ValType VB$t_struct$N0)
IL_0000: nop
IL_0001: ldc.i4.5
IL_0002: stloc.1
IL_0003: ldloc.1
[color=red]IL_0004: box [mscorlib]System.Int32[/color] IL_0009: stloc.0
IL_000a: ldc.i4.5
IL_000b: box [mscorlib]System.Int32
IL_0010: stloc.s unboxObject
IL_0012: ldloc.s unboxObject
IL_0014: call int32 [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.Conversions::ToInteger(object)
IL_0019: stloc.3
IL_001a: ldloc.s vt
[color=red]IL_001c: box BoxingVB.BoxingVB.ValType[/color]
IL_0021: stloc.2
IL_0022: ldloc.2
IL_0023: dup
IL_0024: brtrue.s IL_002b
IL_0026: pop
IL_0027: ldloc.s VB$t_struct$N0
IL_0029: br.s IL_0035
[color=blue]IL_002b: unbox BoxingVB.BoxingVB.ValType[/color]
IL_0030: ldobj BoxingVB.BoxingVB.ValType
IL_0035: stloc.s vtUnBox
IL_0037: nop
IL_0038: ret
} // end of method BoxVB::Main