implicit cast for enums

M

Martin Bischoff

Hi,

is it possible to define an implicit user-defined type conversion
operator for enums?

I'd like to convert my enum to an int without an explicit cast, e.g.
something like this:

------
public enum MyEnum : int
{
One = 1, Two, Three
}

....

MyEnum e = MyEnum.Two;
int i = (int)e; // this works
int j = e; // is it possible to achieve this
 
N

Nicholas Paldino [.NET/C# MVP]

Martin,

It is not possible to do this because enumerations are sealed when they
are created by the compiler.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top