Boxing and UnBoxing VB.NET vs C#

A

Andy Read

Dear all,

From the following examples. Can anyone please tell me why the C# IL
produces the unbox statement and the VB doesn't?

* Forgive me if the code isn't perfect, I don't have VS.NET on my internet
machine!

VB

Dim X as Integer
Dim Y as Object = X '** Box here.
X = CType(X,Integer) '** No Unbox here??


C#

int X;
Object Y = X; // Box here
X = (int) Y; // Unbox here

Many Thanks

Andy Read
 
M

Mattias Sjögren

Andy,
From the following examples. Can anyone please tell me why the C# IL
produces the unbox statement and the VB doesn't?

If you use the DirectCast operator intead of CType, you'll get an
unbox instruction. CType calls into the VB library to support
conversion in addition to casting and unboxing.



Mattias
 
A

Andy Read

Thanks Mattias

Mattias Sjögren said:
Andy,


If you use the DirectCast operator intead of CType, you'll get an
unbox instruction. CType calls into the VB library to support
conversion in addition to casting and unboxing.



Mattias
 
A

Andy Read

Thanks Mattias

Mattias Sjögren said:
Andy,


If you use the DirectCast operator intead of CType, you'll get an
unbox instruction. CType calls into the VB library to support
conversion in addition to casting and unboxing.



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top