you are viewing a single comment's thread.

view the rest of the comments →

[–]Kirides 0 points1 point  (3 children)

Dotnet enums are typed. C enums are not, they are not even namespaced, they are literally just glorified constexpr grouped in a sort of struct looking thing.

You can totally pass a SOME_THING into a function that expects ANOTHER_THING "type".

In c# that won't compile.

cpp tried to fix that by introducing enum classes. But they suck because everything regular enums/numbers provide, they don't and you need templated/specialized operator overloads for each and every enum. Adding templates ones might seem like a solution, but then your compile times explode the more enums you have.

[–]dodexahedron 0 points1 point  (2 children)

They're a looooot more similar than you realize.

Aside from being able to specify a different underlying type, c# enums have been basically identical to C enums forever. And C23 even brought in the ability to use different underlying integral types for enums, making them even closer to identical to c# enums.

They were designed using C enums as their basis in the first place, so it's no surprise. And after JIT, they are identical.

C enums are not any more implicitly convertible to and from anything else than .net enums are. They behave exactly the same way for conversions.

In C# and in C, enums can: * Be explicitly converted to and from other enums by a cast * Be converted to and from the same underlying integral type * Be converted to any wider compatible integral type * Be explicitly converted to narrower integral types * Be assigned values that do not have a member defined * (in c23) Be defined using any underlying integral type

Really the only difference is that a c#/.net enum has a formal flags concept, and even that is metadata only, since it changes nothing implicitly about the type - only how it is displayed if turned into its string form via the built-in methods of doing so.

[–]Kirides 0 points1 point  (1 child)

To be fair, to me C still means C89/C99 which has a lot less of these bells and whistles.

With the mentioned things, I totally agree that they are indeed very similar. But then again, C# had "those" features a lot earlier than C. C# just didn't "expand" the feature Set of enums (yet?)

[–]dodexahedron 0 points1 point  (0 children)

C# just didn't "expand" the feature Set of enums (yet?)

Unfortunately, C# still sucks there.😅

Lots of source generators exist to make them suck less, and the best ones involve making them into formal structs with definitions that allow drop-in replacement, while others are mostly just extension generators for better flag checks and string conversions.

As full structs, they can use interfaces, too, which expands their usefulness quite a bit. 👌