Strongly typed enums

  • Thread starter Thread starter Sathyaish
  • Start date Start date
S

Sathyaish

I forgot the syntax for strongly typed enums in .NET. I know C#
supports them. Not sure if strong-typing of enums is CLS compliant but,
can you refresh my memory with the C# syntax and VB syntax, please?

I've been looking in "Professional C#" to no avail yet. I don't
remember where I saw the syntax on an earlier occassion. This is just a
small thing I am trying to memorize.
 
Can't say for C#, but in VB.NET:

Public Enum something As IntegerBasedType
item = number
item= number
item= number
item= number
etc.
End Enum

Note that it is not necessary to manually assign numeric values if you don't
want to. In that case, the values will automatically assigned Integer
values starting from zero.
 
Sathyaish said:
I forgot the syntax for strongly typed enums in .NET. I know C#
supports them. Not sure if strong-typing of enums is CLS compliant but,
can you refresh my memory with the C# syntax and VB syntax, please?

In addition to the other replies, yes, enumeration types are CLS compilant.
 
Sathyaish said:
I forgot the syntax for strongly typed enums in .NET. I know C#
supports them. Not sure if strong-typing of enums is CLS compliant
but, can you refresh my memory with the C# syntax and VB syntax,
please?

Also, I should point out that in C#, all enums are "strongly-type" which
means that a varaible declared as an enum type, can only be that enum type:

enum ABC {A, B, C}
enum XYZ {X, Y, Z}
public static void Main()
{
ABC abc = XYZ.X; // error
ABC def = 2; // error
}

I assume what you are actually after is not a "Strongly-typed enum", but "an
enum with a user-specified underlying type"
 
Thanks, everybody.

Just for any one else looking for the same information later, the
syntax, as given by the C# specification is:

enum EnumerationName: DataType
{
elementOne;
elementTwo;
}


e.g.

enum NumeralOrders: uint32
{
tens=10,
hundreds=100,
....
....
millions=1000000,
...
...
thingamajillion=1000000000000
}
 
James Curran said:
Also, I should point out that in C#, all enums are "strongly-type" which
means that a varaible declared as an enum type, can only be that enum
type:

This applies to VB.NET with 'Option Strict On' too.
 
James Curran said:
Also, I should point out that in C#, all enums are "strongly-type" which
means that a varaible declared as an enum type, can only be that enum
type:

enum ABC {A, B, C}
enum XYZ {X, Y, Z}
public static void Main()
{
ABC abc = XYZ.X; // error
ABC def = 2; // error
}

Yes, but you can do this:

enum ABC {A, B, C}
public static void Main()
{
ABC abc = (ABC)42; // no error
}

From the C# spec (14.5): "The set of values that an enum type can take on is
not limited by its enum members. In particular, any value of the underlying
type of an enum can be cast to the enum type, and is a distinct valid value
of that enum type."
http://msdn.microsoft.com/library/d...ry/en-us/csspec/html/vclrfcsharpspec_14_5.asp

Regards,
Marcus
 

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

Back
Top