C ++ to C # code conversion, value type reference issues

First of all, sorry for the probably confusing title, feel free to change it when someone knows a more appropriate one.

I am porting a math library from C ++ to C #. Porting the actual code was done after a couple of days. However, I have struggled to resolve all the bugs with C # classes being reference types versus C ++ classes being value types.

Let me explain: if I have a C ++ class Foo that has a nested Bar class, and if I assign an existing Bar instance to Foo, I am essentially copying it if the C ++ compiler knows how to do it ( IF I am not using pointers, of course)

If I do the same in C # I am assigning a link. This leads to incredibly difficult side effects to find. If in C # my code later modifies my Bar instance after assigning it to Foo, then of course the Foo.Bar objects will be modified as well. Not good.

So my question is, is there an easy way or best practice approach to find the positions in my code that assign the link when I assumed I assigned a copy? I even thought about creating a new object every time the getter is called on the object and copies the values ​​there. Of course, this would mean that I could never again bow to the object. Again, not good.

So, basically what I'm asking is: How can I find every occurrence of the assignment operator (=) where TARGET is a reference type? These are the lines of code that I need to change.

I tried to do it manually by just searching for the '=' character, but after searching thousands and thousands of lines of code, but I still don't see the random target.


source to share

1 answer

You should look into struct

C # function instead class

. Structures are value types.



All Articles