Re: Trying to understand enums
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Tue, 23 Jun 2009 16:35:47 -0700
On Tue, 23 Jun 2009 16:12:01 -0700, AMP <ampeloso@xxxxxxxxx> wrote:
[...]
But when I use :
enum Me
{
MyAge=200
}
Console.WriteLine(Me.MyAge);
I get
MyAge.
Sure, that's the name of the enum value.
I want to use an enum as constant in the same fashion, but I must be
misunderstaning Enums???
Yes. Enums aren't supposed to be a way to declare integer constants. They are a way to declare new, simple value types. It gives you a modicum of type-safety for things that would otherwise be represented as some integer base type. If you want a constant typed as an int, then declare a constant types as an int.
Because they are little more than a thin, type-safe veneer on top of an integer base type, it's true that you can do a lot of integer-like things with them, and of course can even cast to and from integer types. But that doesn't mean that you can expect to use them exactly like you'd use an integer. They are still a completely different type, with semantically non-integer, named values as their actual values.
Pete
.
- Follow-Ups:
- Re: Trying to understand enums
- From: AMP
- Re: Trying to understand enums
- References:
- Trying to understand enums
- From: AMP
- Trying to understand enums
- Prev by Date: Re: How does managed code work?
- Next by Date: Re: Trying to understand enums
- Previous by thread: Trying to understand enums
- Next by thread: Re: Trying to understand enums
- Index(es):
Relevant Pages
|
Loading