4

I have an enum (of underlying type int) with two values, and a method that takes a parameter of that type. Is it possible to cast any int value to my enum type and pass it to the method? If so, what is the advantage of an enum? Isn't it supposed to restrict the choice of values available?

class Program
{
    public void Greeting(MyCode code)
    {
        Console.WriteLine(code);
    }

    static void Main(string[] args)
    {
        var p = new Program();
        var m = new MyCode();

        m = (MyCode) 3;
        p.Greeting(m);
    }
}

public enum MyCode:int
{
   Hello =1,
   Hai
}

3 Answers 3

7

Yes, you can cast any value of the underlying type. The enum type is not a restrictive type in that sense.

The advantages of using enums are:

  • Readability
  • Correctness when you don't explicitly cast
  • Ability to access values via reflection etc

I agree that a more type would be useful too, but it doesn't exist in C#. You can build your own class-based "pseudo-enums" in C# which allow for a restricted set of values (using a private constructor), but:

  • You can't make them non-nullable
  • You can't switch on them
  • You can't use them for constant values (such as optional parameter defaults)
  • The value will always be a reference, which can impact on memory usage (compared with an enum with an underlying type of byte, for example)
  • Preserving the restricted values in the face of serialization is interesting

On the other hand, such types can act polymorphically and have more information than plain enums, which can be useful.

Sign up to request clarification or add additional context in comments.

3 Comments

Re: your last point, can't enum types act just as polymorphically as anything else?
@Justin: No, because you can't make them implement extra interfaces, give them extra methods, specialize behaviour etc. Look into the way Java enums work and you'll see what I mean.
Ah, I see. I thought you were just talking about things like function overloading or generic type parameters. I do wish C# enums could be decorated with interfaces, or at least in some way that would give you finer control over their morphism.
6

Just to add to Jon's answer: You can always use

Enum.IsDefined(typeof(MyCode), value)

To check if the provided value exists in the enums definition.

Comments

2

In many ways, Enums are implemented as syntactic sugar in .NET languages. To quote,

The enum keyword is used to declare an enumeration, a distinct type consisting of a set of named constants called the enumerator list.

In other words, the values of the enum are fields like any other, not an integral selection of values. There is nothing stopping you from casting invalid values into an enum. The most you can do to forcibly ensure the valid content of an enum is using the Enum.IsDefined static method (though you may want to define extension methods if this takes your fancy).

Nevertheless, the purpose of enums isn't to validate user input, so they don't have to be as strict as that. The purpose of enums is to tie a name to a numeric value, and to easily display a list of possible combinations of this sort. And don't forget that their loose construction allows things like Day.Saturday | Day.Friday or more complex couplings. They're still very useful.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.