简体   繁体   中英

c# WindowsForm Point pointer value not changing

As my teacher always told me: "in C# everything are pointers",

I am currently on a project where I use Windows Forms and the Point class that goes with it,

exemple:

            Point a = new Point(200, 200);
            Point b = a;
            a.X = 100;
            Console.WriteLine(b.X);

As 'a' is a pointer, when setting 'b' to 'a' and then changing 'aX' value, bX value should change too right? But I still get 200 as a result.

I would like it to be 100 (keeping a link between them), is there any way to do this in C#?

Thanks

For people that have the same problem:

defined as a class it would work, but not as a structure, overrighting the structure with an equivalent class do the trick.

thanks Klaus for the link it does answer my question and way more so I wanted to clarifie it in a shorter answer

As you've found out, value types are copied by value and not by reference.

However if you want an actual reference to this object you could easily just do it like this.

Point a = new Point(200, 200);
ref Point b = ref a;
a.X = 100;
Console.WriteLine(b.X);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM