title: C# enum array accepting a wrong value
body: <p>I was working on a web service method that will receive an array of ints as parameter, and then, inside the method, I converted the values in the array into enum values, and stored them in a enum list. However, when a value that's not in the enum is passed, it is added to the enum list with no problems. No <code>InvalidCastException</code>, nothing. I made a test project that looks like this:</p>
  
  <pre><code>static class Program
  {
      static void Main(string[] args)
      {
          List&lt;TestValues&gt; values = new List&lt;TestValues&gt;() {
              TestValues.Value1,
              TestValues.Value2,
              (TestValues)15
          };
  
          foreach (var val in values)
              Console.WriteLine(val);
  
          Console.Read();
      }
  
      enum TestValues
      { 
          Value1 = 2,
          Value2 = 4,
          Value3 = 8
      }
  }
  </code></pre>
  
  <p>When I run it, the output is:</p>
  
  <pre><code>Value1
  Value2
  15
  </code></pre>
  
  <p>For my web service, I'll implement a validation, so this will never hapen at all. But... this is weird! Shouldn't the runtime throw a <code>InvalidCastException</code>  or <code>ArgumentOutOfRangeException</code>, or something similar? Since I got a list of enums (and not int values), I want the values to be limitted to the values of the enum (that's what an enum is for).</p>
  
  <p>Am I missing something? Is this a .NET bug, a C# bug or is there something I don't know with enums?</p>