What's the equivalent of Java's enum in C#?
In C programming, an enumeration type (also called enum) is a data type that consists of integral constants. To define enums, the enum keyword is used. enum flag {const1, const2, ..., constN};
Enumerations (or enum types for short) are a thin language wrapper around an integral type. You might want to limit their use to when you are storing one value from a closed set of values.
We can convert an enum to string by calling the ToString() method of an Enum.
In C, an enumeration is just a set of named, integral constants. In Java, an enumeration is more like a named instance of a class. You have the ability to customize the members available on the enumeration.
Full Java enum functionality isn't available in C#. You can come reasonably close using nested types and a private constructor though. For example:
using System; using System.Collections.Generic; using System.Xml.Linq; public abstract class Operator { public static readonly Operator Plus = new PlusOperator(); public static readonly Operator Minus = new GenericOperator((x, y) => x - y); public static readonly Operator Times = new GenericOperator((x, y) => x * y); public static readonly Operator Divide = new GenericOperator((x, y) => x / y); // Prevent other top-level types from instantiating private Operator() { } public abstract int Execute(int left, int right); private class PlusOperator : Operator { public override int Execute(int left, int right) { return left + right; } } private class GenericOperator : Operator { private readonly Func<int, int, int> op; internal GenericOperator(Func<int, int, int> op) { this.op = op; } public override int Execute(int left, int right) { return op(left, right); } } }
Of course you don't have to use nested types, but they give the handy "custom behaviour" part which Java enums are nice for. In other cases you can just pass arguments to a private constructor to get a well-known restricted set of values.
A few things this doesn't give you:
EnumSet
Some of that could probably be done with enough effort, though switch wouldn't really be feasible without hackery. Now if the language did something like this, it could do interesting things to make switch work by making the hackery automatic (e.g. declaring a load of const
fields automatically, and changing any switch over the enum type to a switch over integers, only allowing "known" cases .)
Oh, and partial types mean you don't have to have all of the enum values in the same file. If each value got quite involved (which is definitely possible) each could have its own file.
Enums are one of the few language features that is better implemented in java than c#. In java, enums are full fledged named instances of a type, while c# enums are basically named constants.
That being said, for the basic case, they will look similar. However in java, you have more power, in that you can add behavior to the individual enums, as they are full fledged classes.
is there some feature in particular you are looking for?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With