When you are dealing with structures, you are dealing with types of values.
In the class, this is a reference to the current instance. This allows you to mutate an instance of a class by setting properties / fields in the class.
However, if you are in the structure, everything acts differently. When you are in the struct method, "this" allows you to mutate the structure. However, if you use this in a method, you almost always deal with a copy of the "original" structure.
For example:
struct Test { int i; void Mutate() { this.i += 1; } }
When you use this:
void MutateTest(Test instance) { instance.Mutate(); } { Test test = new Test(); test.i = 3; Console.WriteLine(test.i); // Writes 3 test.Mutate(); // test.i is now 4 Console.WriteLine(test.i); // Writes 4 MutateTest(test); // MutateTest works on a copy.. "this" is only part of the copy itself Console.WriteLine(test.i); // Writes 4 still }
Now part of the stranger is really what this quote said:
struct Test { public Test(int value) { this.i = value; } int i; void Mutate(int newValue) { this = new Test(newValue);
Reed copsey
source share