strange compilation warning

  • Thread starter Thread starter Andy Fish
  • Start date Start date
A

Andy Fish

The following code:

enum Foo { one, two };
int Bar(Foo foo)
{
switch (foo)
{
case Foo.one:
return 7;
case Foo.two:
return 4;
}
}

gives a compilation warning "not all paths return a value"

surely this is spurious though? - there is no path I can see that does not
return a value

Andy
 
there is no path I can see that does not return a value
How about:
Foo foo = (Foo) 17; // this works just fine
Bar(foo); //boom

Best option: add a
default: throw new ArgumentException();
line...

Marc
 
Marc Gravell said:
How about:
Foo foo = (Foo) 17; // this works just fine
Bar(foo); //boom

Best option: add a
default: throw new ArgumentException();
line...

Marc

Ooh - I didn't know you could cast an int to an enum even if it was out of
range

Thanks Marc
 
Andy said:
Ooh - I didn't know you could cast an int to an enum even if it was out of
range

C# enum is almost just an int.

If I were to guess at why, then I would say that it is necesarry
to support using multiple values in one variable (flags).

Arne
 
Back
Top