9

What I understand unboxing is when I take a object and unbox it to valuetype like the MSDN example:

int i = 123;
object o = i;  
o = 123;
i = (int)o;  // unboxing

So I just was thinking, can a string be unboxed? I think, No it can't because there is no valuetype that can represent a string. Am I right?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Simon Edström
  • 6,461
  • 7
  • 32
  • 52
  • Possible duplicate: http://stackoverflow.com/questions/6423452/boxing-and-unboxing-in-int-and-string – Louis Kottmann Mar 23 '12 at 16:37
  • 2
    @Baboon I read that question before I posted mine. I think my question was more tight realted to the fact that a string cannot be unboxed. The other is more general in my point of view =) Thanks for keeping SO clean anyway ;-) – Simon Edström Mar 23 '12 at 16:40

1 Answers1

16

You're right. A string can't be unboxed because only value types are subject to boxing and unboxing; a string is a reference type.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • Thanks! I was confused some days ago and start reading about it so it was just a control question so I got it ;-) Thanks ! – Simon Edström Mar 23 '12 at 16:37
  • So a string can neither be boxed as this example says in the first line: http://johnbarshinger.wordpress.com/2008/07/02/c-boxing-and-unboxing-cast-as-convert-parse-tryparse/ – Simon Edström Mar 23 '12 at 16:42