i read section 2.3.2 of skeet's book and, understanding, there no such thing true reference in c#, there in c++.
it's interesting note not "by reference" bit of myth innacurate, "objects passed" bit. objects never passed, either reference or value. when reference type involved, either variable passed reference or value of argument (the reference) passed value.
see, that's different c++ (i come c++ background) because in c++ can use ampersand directly use object in parameter list -- no copies of anything, not copy of memory address of object:
bool iseven ( int & ) { return % 2 == 0 } ) int main () { int x = 5; std::cout << iseven(x); // exact same if had written // std::cout (x % 2 == 0) return 0; } there no equivalent of above. best can in c# equivalent of
bool iseven ( int * ) { return *i % 2 == 0 } ) int main () { int x = 5; std::cout << iseven(&x); // // int * temp = &x; // return *temp % 2 == 0; // (garbage collect temp) return 0; } which passing in value of sort of reference (a pointer) , of course pointless in example because that's being passed in small primitive (int).
from understand, there no c# syntax explicitly designates element being reference, no equivalent of & in c++ example. way know whether you're dealing value or reference memorizing types of elements references when copied , types values. it's javascript in regard.
please critique understanding of concept.
in c#, classes reference types , else (i think) isn't.
there 2 different concepts here:
reference types, "reference" value refers class instance, and
passing reference, "reference" something refers variable.
the word "reference" means different things in 2 contexts , it's important keep them separate.
iirc, skeet has explanation of difference between variables, names, values, , different meanings of "reference".
(if picture variable box can put things, , references pieces of string, first "reference" piece of string tied thing , second "reference" string tied box.)
(and c++ reference parameters are implemented passing address — it's simplest, efficient way refer stored elsewhere.)
Comments
Post a Comment