tags:

views:

895

answers:

8

Are C# enums typesafe?

If not what are the implications?

+10  A: 

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.

Si Keep
A: 

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.

smink
Re integral types: except for the literal 0, which *is* implicit.
Marc Gravell
A: 

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

Scott
A: 

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.

Mike Brown
+13  A: 
Marc Gravell
+1 for the Enum.IsDefined tip.
OregonGhost
Oh, this is a good tip. I did not know this.. always assumed it would throw an InvalidCastException or something. Definitely +1 from me!
Isak Savo
+1. Enum.IsDefined is a great unsung hero ...
John Rudy
Should be -1 for the Enum.IsDefined...see my response here: http://stackoverflow.com/questions/208404/are-c-enums-typesafe#209021
Scott Dorman
I retract my above comment ...
John Rudy
A: 

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.

John Rudy
+1  A: 

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.

Scott Dorman
Good call; I missed that entire note in my first read of that today.
John Rudy
@John Rudy: Thanks! I knew about those issues already but it's good to be able to point to some documentation about it, especially with an example.
Scott Dorman
A: 

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