Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read Jon Skeet's chapter on references versus values. Still confused

I read section 2.3.2 of Skeet's book and, from my understanding, there is no such thing as a true reference in C#, like there is in C++.

It's interesting to note that not only is the "by reference" bit of the myth innacurate, but so is the "objects are passed" bit. Objects themselves are never passed, either by reference or by value. When a reference type is involved, either the variable is passed by reference or the value of the argument (the reference) is passed by value.

See, that's different than C++ (I come from a C++ background) because in C++ you can use the ampersand to directly use an object in a parameter list -- no copies of anything, not even a copy of the memory address of the object:

bool isEven ( int & i ) { return i % 2 == 0 } )

int main ()
{
    int x = 5; 
    std::cout << isEven(x); // is the exact same as if I had written 
                            // std::cout (x % 2 == 0)
    return 0;
}

There is no equivalent of the above. The best you can get in C# is an equivalent of

bool isEven ( int * i ) { return *i % 2 == 0 } )

int main ()
{
    int x = 5; 
    std::cout << isEven(&x); // is like 
                             // int * temp = &x;
                             // return *temp % 2 == 0;
                             // (garbage collect temp)
    return 0;
}

which is passing in the value of a sort of reference (a pointer) and of course is pointless in my example because all that's being passed in is a small primitive (int).

From what I understand, there is no C# syntax that explicitly designates an element as being a reference, no equivalent of the & in my C++ example. The only way you know whether you're dealing with a value or reference is by memorizing what types of elements are references when copied and what types are values. It's like JavaScript in that regard.

Please critique my understanding of this concept.

like image 600
user5124106 Avatar asked Jul 21 '15 04:07

user5124106


1 Answers

In C#, all classes are reference types and everything else (I think) isn't.

There are two different concepts here:
reference types, where a "reference" is a value which refers to a class instance, and
passing by reference, where a "reference" is something which refers to a variable.

The word "reference" means slightly different things in the two contexts and it's important to keep them separate.
IIRC, Skeet has a good explanation of the difference between variables, names, values, and the different meanings of "reference".

(If you picture a variable as a box where you can put things, and references as pieces of string, the first "reference" is a piece of string tied to a thing and the second "reference" is a string tied to a box.)

(And C++ reference parameters are implemented by passing the address — it's the simplest, most efficient way to refer to something stored elsewhere.)

like image 64
molbdnilo Avatar answered Oct 19 '22 22:10

molbdnilo