Does casting null to string cause boxing?
boxing, c#
Solution
Let's take your question and pick it apart.
Will the code in your question cause boxing?
No, it will not.
This is not because any of the 3 statements operate differently (there are differences though, more below), but boxing is not a concept that occurs when using strings.
Boxing occurs when you take a value type and wrap it up into an object. A `string` is a reference type, and thus there will never be boxing involved with it.
So boxing is out, what about the rest, are the three statements equal?
These two will do the same:
var str = (String)null;
String str = null;
The third one (second one in the order of your question though) is different in the sense that it only declares the `str` identifier to be of type `String`, it does not specifically initialize it to `null`.
However, if this is a field declaration of a class, this will be the same since all fields are initialized to defaults / zeroes when an object is constructed, and thus it will actually be initialized to `null` anyway.
If, on the other hand, this is a local variable, you now have an uninitialized variable. Judging from the fact that you write `var ...`, which is illegal in terms of fields, this is probably more correct for your question.
Problem
Imagine code like this: ``` var str = (String)null; ``` Does it differs from: ``` String str; ``` Or: ``` String str = null; ``` Does the first code cause boxing of null value, or is it rather resolved at compiler time to string?