tag:blogger.com,1999:blog-2612479901176767501.post313626913090467167..comments2023-04-10T20:28:23.129+12:00Comments on Blog.Rees.Biz: Casting EnumsBenhttp://www.blogger.com/profile/13689311571100426106noreply@blogger.comBlogger1125tag:blogger.com,1999:blog-2612479901176767501.post-43018293474987835262011-02-01T22:42:53.806+13:002011-02-01T22:42:53.806+13:00Note that the Enum.ToObject(...) method allows you...Note that the Enum.ToObject(...) method allows you to cast a value that does not exist in an enumeration into the enum type. Ie this code compiles and runs with no exceptions:<br /><br /><br /> public enum Enum1<br /> {<br /> Test = 1,<br /> Apple = 2,<br /> }<br /><br /> public static class Program<br /> {<br /> public static void Main(string[] args)<br /> {<br /> int value1 = 0;<br /> int value2 = 1;<br /><br /> var enum1 = (Enum1)Enum.ToObject(typeof(Enum1), value1);<br /> var enum2 = (Enum1)Enum.ToObject(typeof(Enum1), value2);<br /> }<br /> }<br /><br /><br />You'll need to also use the Enum.IsDefined(...) to check all is well.Benhttps://www.blogger.com/profile/13689311571100426106noreply@blogger.com