c# - ref and out with value type variables -
msdn documentation on out says parameter passed out must assigned value inside function. example website:
class outexample { static void method(out int i) { = 44; } static void main() { int value; method(out value); // value 44 } }
according understanding when int "value" declared assigned default value of 0 (since int value type , cannot null.) why necessary "method" modify value?
similarly, if "ref" used instead of out, there need of initializing "value"?
there questions what's difference between 'ref' , 'out' keywords? no 1 wants put 2 , 2 together.
according understanding when int "value" declared assigned default value of 0 (since int value type , cannot null.)
no, that's not correct.
a local variable isn't assigned value when created. space local variables created moving stack pointer make room them on stack. memory area not cleared, contain values happened there.
the compiler forces assign value variable before can used, "garbage" value contains never used.
similarly, if "ref" used instead of out, there need of initializing "value"?
the value wouldn't need set in method, has value. variable used call method need initialised though:
static void method(ref int i) { // doesn't need set value } static void main() { int value; value = 42; // needs initialised before call method(ref value); // value still 42 }
Comments
Post a Comment