EvilRoslynAnalyzers icon indicating copy to clipboard operation
EvilRoslynAnalyzers copied to clipboard

Feature idea: ban structs

Open impworks opened this issue 10 months ago • 0 comments

Structs are an extremely broken feature which any sane and competent programmer should completely avoid:

  • Are copied by value, leading to unexpected behavior, e.g. arrayOfStruct[1].Field = 1 is fine but listOfStruct[1].Field = 1 is not
  • Can sometimes be created in an invalid state because the constructor was not called (runtime, you had one job!)
  • Do not support inheritance, which contradicts the OOP priciples on which C# is built
  • Introduce a lot of keywords like ref, readonly and so on, that blow up the language grammar and put cognitive load on the programmer
  • The readonly modifier does not even guarantee stuff inside being readonly, as you can put a List<T> into a readonly struct and add elements to it at any point

The suggested fix will be to change all struct declaration to class.

impworks avatar Apr 17 '24 08:04 impworks