I have an enum (of underlying type int) 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?
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
}