Hacker News new | ask | show | jobs
by jaywalk 1672 days ago
As someone who works in C# and finds enums useful, I'd be curious to know why you describe them as literally useless.
1 comments

A C# enum is, like a C enum (as they were explicitly introduced to be compatible with those), just a bunch of constants for integers.

So when you have an enum-typed value, odds are good that it’s one of the named ones but there’s no mechanism anywhere preventing it to be any other integer of the underlying type.

But how is that an issue? I guess when you are casting random integer to the enum-type without any checks?
> But how is that an issue? I guess when you are casting random integer to the enum-type without any checks?

Any caller can send any garbage (if you're publishing a package / API), likewise a dependency can return any garbage, etc... C#'s enums are entirely indicative.

Then you have the default path, throwing an exception. I don’t see how that is different from accessing an array with an OOB index.