2

Update Jan 5, 2024: the answer at this link appears to show that the C# compiler completely removes the object cast when trying to cast a string to an object: https://stackoverflow.com/a/51381643/3163495

So, the answer is definitely no, strings do not get boxed.


Original Question:

I understand that value types like int or bool in C# get boxed, like in this example:

int i = 5;
object o = i; // boxing occurs

But do I need to worry about C# boxing string types as well?

string e = "hello world";
object o = e; // does boxing occur here?

I ask because string types are reference types with value semantics, so I'm unsure.

6
  • 2
    What's the question behind this question? String is a reference type but you can avoid boxing even with value types if you use generic parameters and containers instead of object Commented Jun 30, 2023 at 14:45
  • What is your source for the assertion that System.String has value semantics? Strings are immutable, but they do not have value semantics. Commented Jun 30, 2023 at 14:47
  • @MarkBenningfield The answer at this link and others to the same question say that strings have value semantics: stackoverflow.com/a/19838159/3163495 Commented Jun 30, 2023 at 14:49
  • 1
    Avoid using the object type in the first place, and then you don't worry about boxing so much. Commented Jun 30, 2023 at 14:52
  • 2
    "But do I need to worry about C# boxing string types as well?" This smells like micro-optimization. No, boxing is usually not something you need to worry about, except in some very specific edge cases.
    – Heinzi
    Commented Jun 30, 2023 at 15:10

3 Answers 3

4

String is a reference type (Strings and string literals doc, Built-in reference types: string doc), boxing applies only to value types - docs:

Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type.

so no, they are not boxed when they are cast to object, it is so called implicit reference conversions:

In other words, while a reference conversion can change the type of the reference, it never changes the type or value of the object being referred to.

So basically string's are not boxed (or always boxed as soon as you create them - as correctly mentioned by @Charlieface in the comments).

0
0

That's not boxing at all. String is a reference type, only value types can be boxed. String already lives in heap.

Object o = e is upcasting since object is root for all type

1
  • Strictly speaking System.Object is the "root type" for value types too - check out the inheritance hierarchy of ValueType - the base class for value types.
    – Guru Stron
    Commented Jun 30, 2023 at 15:56
-1

Notwithstanding the "mental laziness" of some sources, strings do not have value semantics. Immutability and value equality are not the primary characteristics of value semantics.

Local values are allocated on the stack, not the heap. All instances of a value are the same size. On assignment, the actual value is copied. Strings do none of these things.

So, to say that they have value semantics is "fudging" the definition by quite a bit, leading to the kinds of uncertainty which your question presents.

Not the answer you're looking for? Browse other questions tagged or ask your own question.