c# - Does casting null to string cause boxing? -
imagine code this:
var str = (string)null; does differs from:
string str; or:
string str = null; does first code cause boxing of null value, or rather resolved @ compiler time string?
let's take question , pick apart.
will code in question cause boxing?
no, not.
this not because of 3 statements operate differently (there differences though, more below), boxing not concept occurs when using strings.
boxing occurs when take value type , wrap object. string reference type, , there never boxing involved it.
so boxing out, rest, 3 statements equal?
these 2 same:
var str = (string)null; string str = null; the third 1 (second 1 in order of question though) different in sense declares str identifier of type string, not initialize null.
however, if field declaration of class, same since fields initialized defaults / zeroes when object constructed, , initialized null anyway.
if, on other hand, local variable, have uninitialized variable. judging fact write var ..., illegal in terms of fields, more correct question.
Comments
Post a Comment