This fails at compile-time: public enum MyEnum : short { ValueOne = 0, ValueTwo = 1, }
Console.WriteLine((MyEnum)Int32.MaxValue); John. >-----Original Message----- >From: Moderated discussion of advanced .NET topics. [mailto:ADVANCED- >[EMAIL PROTECTED] On Behalf Of Daniel O'Connell >Sent: Saturday, 10 April 2004 4:56 AM >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