Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • A substantial usage case which Microsoft ignores is when one wants a variable of type Foo to encapsulate a fixed collection of independent values (e.g. coordinates of a point) which one will sometimes want to pass around as a group and sometimes want to change independently. I've not found any pattern for using classes which combines both purposes nearly as nicely as a simple exposed-field struct (which, being a fixed collection of independent variables, fits the bill perfectly). Commented Aug 30, 2013 at 17:10
  • 1
    @supercat: I think it's not entirely fair to blame Microsoft for that. The real issue here is that C# as an object-oriented language simply does not focus on plain record types that only expose data without much behavior. C# is not a multi-paradigm language to the same extent that e.g. C++ is. That being said, I also believe very few people program pure OOP, so perhaps C# is too idealistic a language. (I for one have recently begun exposing public readonly fields in my types, too, because creating read-only properties are simply too much work for practically no benefit.) Commented Aug 31, 2013 at 9:22
  • 1
    @stakx: There's no need for it to "focus" on such types; recognizing them for what they are would suffice. The biggest weakness with C# with regard to structs is its biggest problem in many other areas too: the language provides inadequate facilities for indicating when certain transformations are or are not appropriate, and the lack of such facilities drives unfortunate design decisions. For example, 99% of "mutable structs are evil" stems from the compiler's turning MyListOfPoint[3].Offset(2,3); into var temp=MyListOfPoint[3]; temp.Offset(2,3);, a transform which is bogus when applied... Commented Aug 31, 2013 at 22:44
  • ...to the Offset method. The proper way to prevent such bogus code shouldn't be make structs needlessly immutable, but instead to allow methods like Offset to be tagged with an attribute forbidding the aforementioned transform. Implicit numerical conversions too could have been much better if they could be tagged so as to be applicable only in cases where their invocation would be obvious. If overloads exist for foo(float,float) and foo(double,double), I would posit that trying to use a float and a double often shouldn't apply an implicit conversion, but should instead be an error. Commented Aug 31, 2013 at 22:50
  • A direct assignment of a double value to a float, or passing it to a method which can take a float argument but not double, would almost always do what the programmer intended. By contrast, assigning float expression to double without an explicit typecast is often a mistake. The only time allowing implicit double->float conversion would cause problems would be when it would cause a less-than-ideal overload to be selected. I'd posit that the right way to prevent that shouldn't have been forbidding implcit double->float, but tagging overloads with attributes to disallow conversion. Commented Aug 31, 2013 at 23:00