Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the equivalent of Java's enum in C#? [duplicate]

Tags:

java

c#

enums

What's the equivalent of Java's enum in C#?

like image 484
Nosrama Avatar asked Sep 03 '09 22:09

Nosrama


People also ask

Does C have enum class?

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};

Is enum and enumeration same?

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.

Can we convert enum to string?

We can convert an enum to string by calling the ToString() method of an Enum.

How are enumerations in Java superior to enumerations in C?

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.


2 Answers

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:

  • Ordinal support
  • Switch support
  • EnumSet
  • Serialization/deserialization (as a singleton)

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.

like image 59
Jon Skeet Avatar answered Sep 28 '22 02:09

Jon Skeet


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?

like image 37
Chi Avatar answered Sep 28 '22 03:09

Chi