title: Can I cast an int into a C# enum type?
body: <p>I have an enum (of underlying type <code>int</code>) with two values, and a method that takes a parameter of that type. Is it possible to cast any int value to my enum type and pass it to the method? If so, what is the advantage of an enum? Isn't it supposed to restrict the choice of values available?</p>
  
  <pre><code>class Program
  {
      public void Greeting(MyCode code)
      {
          Console.WriteLine(code);
      }
  
      static void Main(string[] args)
      {
          var p = new Program();
          var m = new MyCode();
  
          m = (MyCode) 3;
          p.Greeting(m);
      }
  }
  
  public enum MyCode:int
  {
     Hello =1,
     Hai
  }
  </code></pre>