jrtom: (Default)
[personal profile] jrtom
[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_.)

(no subject)

Date: 23 July 2007 20:29 (UTC)
From: [identity profile] lightning-rose.livejournal.com

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)
From: [identity profile] jrtom.livejournal.com
C# does _allow_ you to pass structs by reference. It's just that objects are passed by reference and structs are passed by value _by default_, and the syntax and other behaviors encourage you to treat them in similar ways. If the default were to pass both by reference (and allow structs to be passed by value) I wouldn't have a problem with it.

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)
darcydodo: (gregorian lamp)
From: [personal profile] darcydodo
It took me a while to reprocess C# as something-like-C++, rather than C-sharp.

(no subject)

Date: 23 July 2007 22:08 (UTC)
From: [identity profile] jrtom.livejournal.com
*chuckle* Yeah, I can see that.

Profile

jrtom: (Default)
jrtom

May 2011

S M T W T F S
1234567
891011121314
1516 1718192021
22232425262728
29 3031    

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated 24 January 2026 00:59
Powered by Dreamwidth Studios