Groups | Blog | Home
all groups > dotnet general > september 2003 >

dotnet general : Boxing and UnBoxing VB.NET vs C#


Andy Read
9/4/2003 7:38:20 PM
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

Mattias Sjögren
9/5/2003 1:47:34 AM
Andy,

[quoted text, click to view]

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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Andy Read
9/5/2003 5:29:34 AM
Thanks Mattias

[quoted text, click to view]

Andy Read
9/5/2003 5:29:34 AM
Thanks Mattias

[quoted text, click to view]

AddThis Social Bookmark Button