3

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.

user3163495
  • 2,425
  • 2
  • 26
  • 43
  • 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` – Panagiotis Kanavos Jun 30 '23 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. – Mark Benningfield Jun 30 '23 at 14:47
  • @MarkBenningfield The answer at this link and others to the same question say that strings have value semantics: https://stackoverflow.com/a/19838159/3163495 – user3163495 Jun 30 '23 at 14:49
  • ^^ Which is just a fancy way of saying "strings are a little different". In the aspect you are concerned about, though you can view them as reference types. – Fildor Jun 30 '23 at 14:51
  • 1
    Avoid using the `object` type in the first place, and then you don't worry about boxing so much. – Joel Coehoorn Jun 30 '23 at 14:52
  • 1
    *"But do I need to **worry** about C# boxing string types as well?"* This [smells like micro-optimization](https://softwareengineering.stackexchange.com/q/80084/33843). No, boxing is usually not something you need to worry about, except in some very specific edge cases. – Heinzi Jun 30 '23 at 15:10

3 Answers3

5

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).

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
1

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

user1924249
  • 540
  • 3
  • 13
  • 38
  • Strictly speaking `System.Object` is the "root type" for value types too - check out the inheritance hierarchy of [`ValueType`](https://learn.microsoft.com/en-us/dotnet/api/system.valuetype?view=net-7.0) - the base class for value types. – Guru Stron Jun 30 '23 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.

Mark Benningfield
  • 2,800
  • 9
  • 31
  • 31