Re: Box and UnBoxing

From: Shayne Wissler (thalesNOSPAM000_at_yahoo.com)
Date: 11/24/04


Date: Wed, 24 Nov 2004 17:01:55 GMT


"Robert Klemme" <bob.news@gmx.net> wrote in message
news:30joq7F31ihdfU1@uni-berlin.de...
>
> "Vijay singh" <vij_singh@hotmail.com> schrieb im Newsbeitrag
> news:90f0abe8.0411240714.42d67d23@posting.google.com...
>> Isn't Box /unboxing in C# similar to casting or something else?
>
> I assume it's the same as in Java and then it's not similar to casting.
> Boxing creates a new instance of a different class, while casting just
> tries to access the same instance as another type.
>
> In Java:
>
> # Boxing
> Object o = new Integer( 5 );
>
> # Casting
> Number n = (Number) o;
>
> I assume it's similar in C# as that has the distinction between Objects
> and POD's, too.

In C/C++, casting primitives creates a new instance of a different type.
With user-defined type conversion, new objects are created.

I think Java is a bad language to compare this feature with, since with Java
you only have pointers to objects not objects. C++'s semantics are closer to
C# on this point.

Shayne Wissler
http://www.ouraysoftware.com



Relevant Pages

  • Re: What is "evil" about casting?
    ... > in Java 1.5 and there I read more than once that casting was considered ... > Could someone give me a brief explenation as to what is wrong with ... the more robust Generics syntax. ...
    (comp.lang.java.programmer)
  • Re: What is "evil" about casting?
    ... > see what's wrong with casting, in all the books I've read on Java casts ... The problem with casting is that it results in runtime exceptions. ... Generics makes Collections typesafe *and* convenient ...
    (comp.lang.java.programmer)
  • Re: Box and UnBoxing
    ... >> Boxing creates a new instance of a different class, while casting just ... Same for Java. ... Kind regards ...
    (comp.object)
  • Re: What is "evil" about casting?
    ... > in Java 1.5 and there I read more than once that casting was considered ... They then must cast, ... but they require the developer to know the type of object he ...
    (comp.lang.java.programmer)
  • Re: Array Help!?!?!?...
    ... but I'm sure it can be done somehow, ... possibly by casting it to some generic type. ... Or perhaps that's the Java in ...
    (microsoft.public.word.vba.general)