Read the John Skeet chapter on links versus values. Still confused

I read section 2.3.2 of Skeet’s book and, in my opinion, in C # there is no such thing as a true link, as in C ++.

It is interesting to note that not only the “by reference” bit is a genuine myth, but also “objects are passed”. They are never passed objects by reference or by value. When a reference type is used, either the variable is passed by reference or the value of the argument (link) is passed by value.

See what is different from C ++ (I proceed from the background of C ++), because in C ++ you can use an ampersand to directly use an object in the parameter list - there are 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; } 

No equivalent above. The best you can get in C # is equivalent

 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 conveys the value of a kind of link (pointer) and, of course, is senseless in my example, because everything that is passed is a small primitive ( int ).

From what I understand, there is no C # syntax that explicitly indicates the element as a link, not the & equivalent in my C ++ example. The only way to find out if you are dealing with a value or a link is to remember which types of elements are links when copying and which types are values. This is similar to JavaScript in this regard.

Please criticize my understanding of this concept.

+7
c ++ optimization reference c #
source share
2 answers

In C #, all classes are reference types, and everything else (I think) is not.

There are two different concepts here:
reference types, where “reference” is a value that refers to an instance of the class, and
passing by reference, where “link” is what refers to the variable.

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

(If you represent a variable as a field where you can put things, and links as fragments of a string, the first “link” is the part of the string attached to the subject, and the second “link” is the string bound to the field.)

(And C ++ control parameters are implemented by passing an address - this is the easiest and most effective way to reference what is stored elsewhere.)

+7
source share

If I understand your question correctly, it’s hard for you to understand if this is a reference or a type value.

An easy way is to know what you are using for the call or structure.

All classes are reference types, and the entire structure is value types. Even int is a structure

  public struct Int32 

Same way bool also

  public struct Boolean 

To find out more about the class, you must press 'f12' on the keyword.

+2
source share

All Articles