Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>_counter is an int and _message is a string.</p> </blockquote> <p>Which are .NET types that will defeat your plan. An <em>int</em> is a value type, when you pass it to a method that takes an argument of type <em>object</em> then the int will be <em>boxed</em>. You'll get a <strong>copy</strong> of the value. Updating that copy will never have an effect on the original value, no matter what you do.</p> <p>Similar story with <em>string</em>, it is an <strong>immutable</strong> reference type. It cannot be changed in any way, you can only create a <em>new</em> string object from the original. A new string that the original code knows nothing about of course.</p> <p>Getting updates to be visible in other code requires <em>pointers</em>. Pointers are pretty tricky, C# supports them well enough but creating a pointer to a variable has a lot of bear traps. You can for example never create a pointer to a local variable and expect that pointer to still work when the method has exited. The local variable doesn't exist anymore. Updating the value through the pointer anyway will <em>corrupt</em> memory. A big reason you need to use the <em>unsafe</em> keyword when working with pointers.</p> <p>Pointers are already ably wrapped in C#, they are called <em>references</em>. And are automatically used when you create an object of a reference type. So what you should do is not just pass simple value or strings, you should pass a reference to an object instead. A class object. Now both your debugging code and your original code are using a reference to the exact same object. And updates made to fields and properties of the object in one are visible to the other.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload