0

Inside a class, I have an enum and a private int:

 public enum DOOR_STATE { IDLE = 0, OPENING, OPEN, CLOSING };
 private int currentState;

Inside a method, if I do:

currentState = DOOR_STATE.OPENING as int;

I get an error message: "The as operator cannot be used with a non-nullable value type int"

But if instead I do:

currentState = (int) DOOR_STATE.OPENING;

The error goes away.

Could someone please explain the difference between the two casting methods ?

Running Turtle
  • 12,360
  • 20
  • 55
  • 73
  • 1
    [as](https://msdn.microsoft.com/en-us/library/cscsdfbt.aspx): "if the conversion isn't possible, **as** returns **null** instead of raising an exception" - since `int`s can never be null, that's why you can't use it. – Damien_The_Unbeliever Jun 23 '15 at 13:08
  • There are already various questions like this... http://stackoverflow.com/questions/496096/casting-vs-using-the-as-keyword-in-the-clr – xanatos Jun 23 '15 at 13:08

0 Answers0