Can I unbox a string?
Asked Answered
N

1

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?

Ng answered 23/3, 2012 at 16:34 Comment(2)
Possible duplicate: #6423952Illinois
@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 ;-)Macadamia
O
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.

Oldtimer answered 23/3, 2012 at 16:35 Comment(2)
Thanks! I was confused some days ago and start reading about it so it was just a control question so I got it ;-) Thanks !Macadamia
So a string can neither be boxed as this example says in the first line: johnbarshinger.wordpress.com/2008/07/02/…Macadamia

© 2022 - 2024 — McMap. All rights reserved.