Triabolical_ t1_ixigt2n wrote
Reply to comment by oneeyedziggy in ELI5: What is the difference between classes, constructors, structs and methods in C# by Sure-Ad-5053
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.
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)?
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.
Viewing a single comment thread. View all comments