Viewing a single comment thread. View all comments

Triabolical_ t1_ixg5avj wrote

>I'm not a C# dev so while the rest may be close enough, Imma have to just have a go at describing structs: I'm pretty sure they just typed sets of properties... Like a class without methods... Like you might instead make a pie struct (because honestly, pies probably don't need methods, they don't DO a lot, and they're probably not dynamic or complex enough to need a constructor) so then you just make a "feast" class that has a bunch of food struct-typed properties with single eat method that takes the name of a food and subtracts from its quantity

Structs are value types, while classes are reference types.

Structs exist in C# so that you can easily implement types with value semantics - like a bignum class - and to do interop with windows system APIs that were written with C and therefore require a very specific memory layout.

Struct *mostly* operate like classes; both can have methods with them.

1

oneeyedziggy t1_ixgd04t wrote

Huh... TIL... you'd think rather than coming up with a whole new name they'd just add another declaration keyword to do whatever the non-default thing is...

1

Triabolical_ t1_ixigt2n wrote

I was the test lead for the C# compiler and spent a lot of time in design meetings...

"class" and "struct" come from C++ though there they only have a difference in visibility, not in behavior.

"class" is the obvious choice for the main construct as that's what's used in many other languages.

Then we needed a name for something that's kindof like a class but not the same thing. It turns out that coming up with names for things in languages is really hard, as many of the non-reserved words are already used by programmers in a lot of cases. C# was a new language at that time but there were already developers writing the base class libraries in C# and their feedback was "don't take away names from us".

They were virtually all C++ developers, and nobody who writes C++ code was going to call something "struct" because it was a reserved word, so it was available, and we wanted something very distinct from "class" because the intended usage was so different.

That was a pretty early decision during language design.

Later the team came up with the concept of "conceptual keywords" - keywords that were reserved but only in specific situations. The words "get" and "set" for properties are a good example of that. If we'd done that earlier, we might have done something different with struct though I think we probably would have stuck with it as a name.

Not that the conceptual difference between classes and structs isn't quite confusing and very different than how C++ views the world, but that was inherently going to happen moving from an unmanaged to a managed world.

Thanks for letting me take a trip back on memory lane.

2

oneeyedziggy t1_ixilahp wrote

Awesome context... As I'm not a c# dev (well, my professional use of c# was extremely short lived and 10+ years ago...)... I was thinking in terms of what little java I know (also not a java dev) but how basically all options 9n a variable are enumerated in long-form when declaring ( public static String blah blah...) and just imagining (naively, based on the previous comment) that it might have been as simple as adding a byref or byval to the string of qualifiers when using a class to get struct behavior (and defaulting to some sane default where omitted)... Is this even relevant to the situation either in c# having such qualifiers or in classes and structs being sufficiently similar for that to be feasible (if only everything had been different)?

1

Triabolical_ t1_ixjwabm wrote

> Is this even relevant to the situation either in c# having such qualifiers or in classes and structs being sufficiently similar for that to be feasible (if only everything had been different)?

C++ is pretty much that way; structs and classes are mostly the same thing and the developer chooses whether they want to allocate it on the stack or whether they want to put it in dynamic memory.

In C# - and managed languages in general - class instances are always allocated in dynamic memory. Structs were really just added for windows interop, though they do have other limited uses.

2