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.
object
System.String
has value semantics? Strings are immutable, but they do not have value semantics.object
type in the first place, and then you don't worry about boxing so much.