How does the swap method work in C # at the memory level?

I am a Java programmer. I have little knowledge about C #. But from the blogs I read, Java only supports pass-by-value-of-reference, while in C # the default is pass-by-value-link, but the programmer can use pass-by-link if necessary.

I understood my understanding of how the swap function works. I think it is very important to understand this concept, as it is very important for programming concepts.

In C#:

 public static void Main() { String ONE = "one"; //1 ChangeString(ONE); //2 Console.WriteLine(ONE); //3 String ONE = "ONE"; //4 ChangeString(ref ONE); //5 Console.WriteLine(ONE); //6 } private static void ChangeString(String word) { word = "TWO"; } private static void SeedCounter(ref String word) { word = "TWO"; } 
  • Step 1: A string object with the value one is created on the heap, and the address of its location is returned in the variable one . The Runtime Environment allocates a chunk of memory on the heap and returns a pointer to the beginning of this memory block. This one variable is stored on the stack, which is a reference pointer to search for the actual object in memory

  • Step 2: The changeString method is changeString . Copies of the pointer (or location of the memory address) are assigned to the variable word. This variable is local to the method, which means that when the method call ends, it is removed from the stack frame and out of scope for garbage collection. In the method call, the variable word is reassigned to indicate the new location where the TWO object is in memory. method returns

  • Step 3: Printing on the console should print one , because what was changed in the previous step was only a local variable

  • Step 4: Variable one is reassigned to indicate the memory location where one is located.

  • Step 5: the changeString method is changeString . This time refers to one . this means that the local variable of the method word is an alias of the variable in the main area. Therefore, a copy of the link is not made. Therefore, this is equivalent to the thought that the same variable is passed to the method call. The method reassigns a variable pointing to another memory location that takes place TWO . Method returns

  • Step 6: Now the variable one in the outer scope, that is, in the main method, is changed by calling the method, and therefore it prints TWO .

In Java step 5 is not possible. That is, you cannot pass by reference .

Please correct if the programming flow described above is correct?


The articles I read are here and here .

+6
source share
2 answers

You seem to understand the semantics correctly. Just draw some analogues in other languages ​​..

In C #, an object reference is used by default (one level of indirection), except for value types. Passing by reference is essentially the transfer of a pointer to a link to an object, which is a double indirectness.

The closest is the analogy of C or C ++.

C ++

 void ChangeString(std::string word) { word = "TWO"; } void SeedCounter(std::string &word) { word = "TWO"; } 

C (Ignoring const problems, etc.)

 void ChangeString(char * word) { word = strdup("TWO"); } void SeedCounter(char ** word) { *word = strdup("TWO"); } 

But the Java analogy should probably be a class with a string element:

 public class StringRef { public string val; } public static void ChangeString(string word) { word = "TWO"; } public static void SeedCounter(StringRef strRefWord) { strRefWord.val = "TWO"; } 

Development of each request.

In C # (or in the CLR more specifically), a string variable is a pointer, but we call it an object reference. The variable contains an address that points to a string object, usually somewhere on the heap. The variable itself is usually either a class field, where it probably exists on the heap, or a local variable or argument, so it exists on the stack or in the local slot of the variable (also on the stack). When you follow a link, you pass a pointer to your variable, not a pointer to the destination. Your "ref" parameter is A, A points to B, which is your local variable or object field, and B points to C, a string object somewhere in memory. Passing with ref passes A, which is a pointer to B, and now you can change B.

+3
source

Your understanding of the semantics of going through a link / value is correct.

0
source

Source: https://habr.com/ru/post/975502/


All Articles