Question

Are C# enums typesafe?

If not what are the implications?

Was it helpful?

Solution

To give a slightly different answer... while the values are type-safe from the casting perspective, they are still unchecked once they have been cast - i.e.

enum Foo { A = 1, B = 2, C = 3 }    
static void Main()
{
    Foo foo = (Foo)500; // works fine
    Console.WriteLine(foo); // also fine - shows 500
}

For this reason, you should take care to check the values - for example with a default in a switch that throws an exception.

You can also check the (for non-[Flags] values) via:

bool isValid = Enum.IsDefined(typeof(Foo), foo);

OTHER TIPS

Yes they are.

The following is from http://www.csharp-station.com/Tutorials/Lesson17.aspx

Enums are strongly typed constants. They are essentially unique types that allow you to assign symbolic names to integral values. In the C# tradition, they are strongly typed, meaning that an enum of one type may not be implicitly assigned to an enum of another type even though the underlying value of their members are the same. Along the same lines, integral types and enums are not implicitly interchangable. All assignments between different enum types and integral types require an explicit cast.

For those suggesting to use Enum.IsDefined to do argument validation...don't! Per Brad Abrams (from the Framework Design Guidlines Update on Enum Design):

There are really two problems with Enum.IsDefined(). First it loads reflection and a bunch of cold type metadata making it a deceptively expensive call. Secondly, as the note alludes to there is a versioning issue here.

Yes they are.

Enums are strongly typed constants. They are essentially unique types that allow you to assign symbolic names to integral values. In the C# tradition, they are strongly typed, meaning that an enum of one type may not be implicitly assigned to an enum of another type even though the underlying value of their members are the same. Along the same lines, integral types and enums are not implicitly interchangable. All assignments between different enum types and integral types require an explicit cast.

Yes.

C#: enum types:

-A type-safe enumeration of named values.

-Prevents programming errors

-User can control underlying type (defaults to int)

-Also can control underlying values

Technically no because you can represent an Enum as its base value (int, long, etc). However, if you ensure that you only use the enum by its provided names, you will receive compile time errors if you change the name of an enumerated value without updating its references. In this regard yes it is type safe.

I'm late to the party here, but I wanted to throw out a little something extra ... An update to the .NET Framework Design Guidelines from Krzysztof Cwalina. In addition to the excellent tip above on checking to ensure a valid value is passed to your Enums, this provides other guidance and advice for how to use them effectively, a bunch of gotchas (especially involved Flags enums), etc.

yes they r strongly-typed safed u cannot it does not do impilcit convertion of enum constant variables to integeral value u hv 2 expilcitly do dat eg enum days { sun, mon } int e = (int ) days.sun; console.writeline(e);

C# enums are type safe meaning

  1. You cannot implicitly convert from the underlying type to the actual enum
  2. Also, you cannot assign a value of one enum to another enum, even though the underlying types of both the enums are same. An explicit cast is always required.
  3. Enums make your code more readable and more maintainable.

I have come accross an excellent video tutorial on enums exaplaining their type safe nature. Have a look here and there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top