4

First off, sorry for the probably confusing title, feel free to change it whenever somebody knows a more appropriate one.

I'm in the process of porting a math library from C++ to C#. Porting the actual code was done in a couple of days. However, I've been struggling to solve all the bugs introduced by the fact that C# classes are reference types versus C++ classes being value types.

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

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

So my question is: Is there an easy way, or a best practice approach, to find the positions in my code that assign a reference when I was assuming I assigned a copy? I even considered creating a new object every time the getter is called on an object, and copying the values there. Of course this would mean that I can never have a reverence to an object again. Again, not good.

So basically what I'm asking is: How can I find every occurrence of the assign operator (=) where the TARGET is a reference type? It's those lines of code I need to change.

I've tried to do it manually by simply searching for the '=' character, but after searching thousands and thousands lines of code, but I'm still missing the occasional assignment target.

JoelFan
  • 37,465
  • 35
  • 132
  • 205
lightxx
  • 1,037
  • 2
  • 11
  • 29
  • try to avoid nested classes to unless you are familiar with using them.. personally I think it can cause you some issues as well JoelFan has an awesome recommendation as well but also know that for very large types I would use a class vs a struct – MethodMan Sep 12 '14 at 15:55
  • Before going any further, make sure you know C# very well... I can't recommend this book enough: http://www.amazon.com/C-Depth-3rd-Jon-Skeet/dp/161729134X/ref=sr_1_10?s=books&ie=UTF8&qid=1410537373&sr=1-10 Any time spent reading this book will be regained many times – JoelFan Sep 12 '14 at 15:56
  • I very much appreciate your inputs. However, I'm on a very limited time budget right know. *That's the reason for porting the library almost 1:1*. I was just asking for some hints / tips / ... for the job at hand – lightxx Sep 12 '14 at 16:00
  • 2
    If you want to save time, read the book :) – JoelFan Sep 12 '14 at 16:09
  • If I had the time I would rewrite the library from the ground up and avoid all those issues altogether ... ;) I just hoped somebody had a very hacky quick and dirty fix like the one in the get method of the property I mentioned above ... – lightxx Sep 12 '14 at 16:10
  • and BTW I have all of Andrew Troelsen's books ... He seems to be kinda arrogant though ... Or probably that's just his writing style ... – lightxx Sep 12 '14 at 16:23

1 Answers1

6

You should look into the struct feature of C# instead of class. Structs are value types.

JoelFan
  • 37,465
  • 35
  • 132
  • 205
  • I thought structs were immutable, no? Or at least I got the impression from reading I thought of structs, but then: http://stackoverflow.com/questions/3751911/why-are-c-sharp-structs-immutable – lightxx Sep 12 '14 at 15:55
  • 1
    @lightxx Mutable structs are evil. They exist, but it's almost certainly a mistake to be using them. Of course, the question you just linked to explains exactly that. – Servy Sep 12 '14 at 15:57
  • 1
    actually ... If I covert my classes to structs and just take a look at where the compiler bombs I probably find out where those assignment targets are ... Hm ... I try that as soon as I'm in the office again ... – lightxx Sep 12 '14 at 16:02
  • 1
    The compiler will not bomb. There is nothing in the C# language that prevents a struct from being mutable. Yes, it's evil for them to be mutable, but it sounds like your program is an evil one (according to C# best practices). Of course, whenever in your C++ program you allocated a class on the heap with the new operator, you would have to use a class in C# too. – Kris Vandermotten Sep 12 '14 at 16:44
  • Aren't all members of a struct public? – pacmaninbw Apr 14 '19 at 15:04
  • 1
    @pacmaninbw I think that is only C++ and only by default. This is about C# – JoelFan Apr 16 '19 at 18:22