3

I know that object enumeration is one of few things that Java have over C#, and sometimes I assume that I miss that feature.

But I've seen some cases that a class is used to represent an enumeration of objects, using constant fields, so it can "return an object", instead of an int.

An example is the Brushes class. It works really like an enum.

Brush b = Brushes.Blue;

We can see that the major difference (at least for me) is the need of other class (the class Brush) to store the value returned from the class Brushes (some code with implicit operator overload can do the trick).

So, my question is: good practice?

Because I'm thinking about use this into my project to make an enum of Animations (WPF), to use into a CustomControl I've created.

It seems good to me, and better than the use of an integer enumeration, because I'll need to check with a switch-case to detect witch Animation the user wants.

Guilherme
  • 5,143
  • 5
  • 39
  • 60
  • 2
    It... doesn't work like an enum. Those are static readonly properties that return frozen `SolidColorBrush`s. Also, "object enumeration is one of few things that Java have over C#" what are you talking about? –  Aug 02 '13 at 04:08
  • As I understood, enums in C# cannot represent anything different than an int; for example java enums can represent strings Look at this question. http://stackoverflow.com/questions/3978654/java-string-enum Sorry if I was unclear, and sorry for my english. – Guilherme Aug 02 '13 at 04:17
  • I also know that this not work as an enum (again I was unclear). I only wants to know if the use of a class like I said is reliable. – Guilherme Aug 02 '13 at 04:22
  • This question http://stackoverflow.com/questions/5250915/enum-with-object-or-a-dictionary-like-enum is what I want to do, and the answer of this question is the use of a class, like as said. – Guilherme Aug 02 '13 at 04:30

2 Answers2

5

Enums are a fundamentally different thing in Java and in C#. In C#, they're simply names for integral types, in Java, they're a lot more like classes with named defaults. In C#, a class with a bunch of static readonly fields or properties is what Java calls an enum, minus the ability to switch on them.

This pattern does show up every once in a while, see System.Drawing.Color and System.Text.Encoding

Like with most things, so long as you don't abuse it, it's good practice. This pattern is generally used to only provide named defaults for a class or struct. Make sure the class can stand on it's own and that you don't heavily rely on the names of those properties. If you need to occasionally check something, then you can probably get away with overloading the == operator.

If you want something exactly like Java's enums, then you should have a C# enum that internally stores the type, and do one of two things:

  1. Make all the constructors require the enum
  2. Create named defaults that set the enum internally

Example:

public enum FooType
{
    A,
    B,
    C
}

public class Foo
{
    public FooType Type { get; private set; }

    // Option 1: forced via constructor

    public Foo(FooType type)
    {
        this.Type = type;
    }

    // Option 2: static properties
    // (using the constructor from option 1, can be done without it, though)

    public static Foo A { get { return new Foo(FooType.A); } }

    public static Foo B { get { return new Foo(FooType.B); } }

    public static Foo C { get { return new Foo(FooType.C); } }
}

Usage:

Foo bar = new Foo(FooType.B);

// OR

Foo bar = Foo.B;

Ability to switch:

switch (bar.Type)
{
    case FooType.A:
    break;
    case FooType.B:
    break;
    case FooType.C:
    break;
}
Robert Rouhani
  • 14,512
  • 6
  • 44
  • 59
  • Yup, just an extra note with the example, don't use it as a way to replace inheritance or anything (that would be messy), I just used `FooType` since it could be anything. – Robert Rouhani Aug 02 '13 at 04:40
3

Yes it is reliable it even has a name. It is the "Singleton pattern". You just create a set of singletons and set them as read only properties of your class. It is used all over the framework.

Community
  • 1
  • 1
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431