Daniel is completely correct in his description of enums.  One additional
note -- if you do wish to validate enum values, validate them only when it
makes sense to do so.  In other words, validate them only when they are
generated through some means other than identifying a member of the enum
(which is guaranteed to be valid) such as casting from an integer, etc.

Also, if you must validate the range of an enum value, consider using
Enum.IsDefined.  This does exactly what you want, and is also amenable to
static analysis, either using SA tools, or running within very smart CLR
implementations.

-- arlie


-----Original Message-----
From: Moderated discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Daniel O'Connell
Sent: Friday, April 09, 2004 2:56 PM
To: [EMAIL PROTECTED]
Subject: Re: [ADVANCED-DOTNET] Enumeration bounds checking?

Because it's perfectly legal as far as the CLR is concerned. An enum is
effectively an integral primitive that is treated slightly differently by
the runtime. Any valid value for the primitive type is a valid value for the
enum.
By not limiting the enum to a set of values, it's possible to support
scenarios where unknown enum values can be ignored or where flags with
unknown bits set can exist. If it's important in your application you can
perform checking yourself pretty easily without bogging down all uses of
enums with needless value checking.

> -----Original Message-----
> From: Moderated discussion of advanced .NET topics. [mailto:ADVANCED-
> [EMAIL PROTECTED] On Behalf Of Michael Sawczyn
> Sent: Thursday, April 08, 2004 3:35 PM
> To: [EMAIL PROTECTED]
> Subject: [ADVANCED-DOTNET] Enumeration bounds checking?
>
> This is curious, and not covered in the C# language spec that I can find.
> There appears not to be bounds checking invoked when enums are passed
> as parameters. The following compiles and runs successfully. Any
> thoughts as to why this would not be a language defect?
>
> public enum E
> {
>    E1,
>    E2
> };
>
> class Class1
> {
>    static void Main()
>    {
>       foo((E)50); // should throw runtime exception?
>    }
>
>    static void foo(E e)
>    {
>       System.Console.Out.WriteLine(e);
>    }
> }
>
> ===================================
> This list is hosted by DevelopMentorR  http://www.develop.com Some
> .NET courses you may be interested in:
>
> NEW! Guerrilla ASP.NET, 17 May 2004, in Los Angeles
> http://www.develop.com/courses/gaspdotnetls
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com

===================================
This list is hosted by DevelopMentor  http://www.develop.com Some .NET
courses you may be interested in:

NEW! Guerrilla ASP.NET, 17 May 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com
Some .NET courses you may be interested in:

NEW! Guerrilla ASP.NET, 17 May 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to