tags:

views:

836

answers:

4

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

+4  A: 

enum , or do you need something in particular that Java enums have but c# doesn't ?

leeeroy
In Java, enums can have multiple associated values for each enum, methods with logic, and constructors. At least as of C# 3.0, C# enums are still just a list of constants, with a possible associated value.
Ogre Psalm33
+9  A: 

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?

Chi
"They will work the same" - not IMO, because constant numbers can't have custom *behaviour*, unlike Java enum values.
Jon Skeet
yup, hence the qualification 'for basic use'.I agree that one of the nice things about java enums is the custom behavior
Chi
+3  A: 

You could probably use the old typesafe enum pattern that we used in Java before we got real ones (assuming that the ones in C# really aren't classes as a comment claims). The pattern is described just before the middle of this page

Bill K
+8  A: 

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

        internal GenericOperator(Func<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.

Jon Skeet