SoFunction
Updated on 2025-03-06

A brief discussion on the issue of C# pointer

It took a long time to practice and finally figured it out. Classes or linked lists, etc., will use a new pointer when assigning a pointer. for example:

Foo a = c;
Foo b = new Foo();
Foo a = b;

In this case, the pointer of b will be passed to a, a will no longer point to c, and future operations of a will take effect on b.
In the following situations:

Foo b = new Foo();
Foo a{get {return b;}}

In this case, it means that a cannot be modified, but if you = 5;, then it can be modified. Why? Because at this time any modification to the attribute of a is essentially a modification to b, only a = c; is not allowed. It can be simply understood that a = c; is the assignment of a pointer, while = 5; is just the property of the object referred to has changed.

In summary:

If you want a to be a stand-in that only points to b, then you can do this method later. The advantage is that you can modify the attribute of a, that is, the attribute of b, at will, but a will always point to b and will not be tampered with.
If you want to use a multiple times, then you can only use the first method. However, if you don't understand it and use it wrong, you will often accidentally modify it to the original value.