c# - Passing arguments, does unboxing occur -
what have read, passing arguments default valuetypes. in example first function test1 takes reference type , unbox, decrease performance if got right. have never read test2 increase performance.
so whats best practice?
public main(){ string test = "hello"; test1(test); // line perform boxing? it's not performance? test2(ref test); // passing reference reference } public string test1(string arg1) { return arg1; } public string test2(ref string arg1) { return arg1; }
there's no boxing or unboxing involved @ here. string
reference type - why boxed? mean?
even if used int
instead, there'd no need boxing, because there's no conversion of value actual object.
i suspect understanding of both boxing , parameter passing flawed.
boxing occurs when value type value needs converted object, in order used variable (somewhere) of interface or object type. this boxes:
int value = 10; foo(value); ... public void foo(object x) { }
... wouldn't occur if foo
changed such type of x
int
instead.
the detailed rules on boxing become complicated state precisely , accurately, particularly generics come in, that's basics.
Comments
Post a Comment