Is it possible to do the following with an enum in C#?:
Pass in to a method a selected value of the enum (eg if an enum has members such as Red, Green, Orange, I can pass in Colors.Red).
In the method body of the above method which accepts an enum, I can say if (Enum == Co开发者_开发百科lors.Red).
What would be the syntax for this? I've always seemed to have stalled on this.
Yes, just like you would with any other type ...
public void MyMethod( Color color )
{
if( color == Color.Red )
...
}
You can do it like this:
public enum Colors{
Red,
Blue
}
...
public void DoSomething(Colors theColor){
if(theColor == Colors.Red) // do something...
}
Yes, you can. This is a simple example:
public enum Colors
{
Red,
Orange,
Green
}
...
public bool IsRed(Colors c)
{
if (c == Colors.Red)
return true;
else
return false;
}
Your function call would look like:
bool test = IsRed(Colors.Green); //false
Yes and yes. Syntax is just as you have there.
精彩评论