C# and structs
23 July 2007 12:58[I don't know of anyone that reads this that will care. But maybe it will prove useful to someone.]
As I've just spent a while being bitten by this, I thought I'd pass this hard-won information along:
In C#, structs are passed by value, not reference (even though they act in many respects like classes).
An example, taken from all-too-real life, in which this becomes a problem:
Suppose that you have a dictionary in which you're keeping structs that contain statistics, stored in fields. Periodically, you retrieve a struct from this dictionary and update one of its statistics. However, since structs are passed by value, however, the struct that you modify is not the same as the one that the dictionary owns, and so the dictionary's struct's statistics are never modified.
That is to say: AAAAAAAARGH! (I mean, seriously: I realize that C# has a lot of adherents that came directly from C/C++, and those languages do the same thing. But why would you _want_ to pass a struct by value? All that stuff dumped on the stack...and structs can be _huge_.)
As I've just spent a while being bitten by this, I thought I'd pass this hard-won information along:
In C#, structs are passed by value, not reference (even though they act in many respects like classes).
An example, taken from all-too-real life, in which this becomes a problem:
Suppose that you have a dictionary in which you're keeping structs that contain statistics, stored in fields. Periodically, you retrieve a struct from this dictionary and update one of its statistics. However, since structs are passed by value, however, the struct that you modify is not the same as the one that the dictionary owns, and so the dictionary's struct's statistics are never modified.
That is to say: AAAAAAAARGH! (I mean, seriously: I realize that C# has a lot of adherents that came directly from C/C++, and those languages do the same thing. But why would you _want_ to pass a struct by value? All that stuff dumped on the stack...and structs can be _huge_.)
(no subject)
Date: 23 July 2007 20:29 (UTC)In C/C++ you have the option of passing pointers (reference) or copies on the stack (value). If C# gives no choice, then I can only suggest you consider the source.
Yes, I'm pretty sure I know where you work. :)
That said, I think this is symptomatic of the dumbing down of computer science that's been going on in universities since the late 80's; coinciding with the rise of oop.
This guy states it mostly well.
http://www.joelonsoftware.com/articles/ThePerilsofJavaSchools.html
(no subject)
Date: 23 July 2007 22:32 (UTC)As to that article...I would suggest that people who have never studied OOP are "missing something" (in the sense that he describes) in the same sense that someone who's never programmed assembly or never used a functional language is missing something. These different kinds of languages have different things to teach you. I agree that a weeding-out process is necessary, and I agree that teaching all in one language is a bad idea, but I could write his essay over again from the perspective of someone who thinks that schools who teach only C (say) are missing out on some essential lessons in the OOP world.
(no subject)
Date: 23 July 2007 20:52 (UTC)(no subject)
Date: 23 July 2007 22:08 (UTC)