Most
of the .NET literature I've come across reads somewhat easily because
it is so similar to Java and I had already taken several Java
classes.
But
one of the things that I find confusing in the .NET literature is
their talk of boxing and unboxing and their emphasis on Value Types
and Reference Types. I couldn't remember
a similar topic in Java. What I do remember from Java was that
almost everything is an object except for certain native (intrinsic
or primitive) types like short, int, float, etc. For efficiency,
these were not objects but there exists wrapper classes for them
(like Integer etc) that can turn them into true objects.
Is
this basically what .NET's boxing and unboxing is?
.NET
literature says that everything is an object. But it's sort of
confusing. Value Types get created on the stack and Reference Types
get created on the CLR heap and boxing basically makes a copy of a
Value Type into a Reference (CLR heap) Type and vice versa. Most of
the time, boxing and unboxing is done automatically in the
background.
To me, Value Types sound a lot like Java's
intrinsic type and boxing is the same thing as providing a wrapper
class to turn it into an object (Reference Type). This is because in
Java an intrinsic type is created on the stack and an object is
created from the Java heap.
Is
this automatic boxing and unboxing what allows Microsoft to say that
everything in .NET is an object? Since it automatically wraps Value
(stack based) Types into Reference Types (CLR heap), the programmer
can think of everything in .NET as an object. However, there are
situations where the programmer has to do boxing or unboxing
manually.
I
sometimes wonder if Microsoft was playing a semantic game so they
could make marketing claims that everything in .NET is an object (and
hence give it a marketing edge over Java). I personally find Java's
concept of wrapper classes an easier concept to understand and wish
that Microsoft had done the same thing in .NET.
Or
am I confused about this topic?