I need to perform this operation many times on my data:
public void AddBehavior(BehaviorTypes type)
{
if (Enum.IsDefined(typeof(BehaviorTypes), type))
{
switch (type)
{
case BehaviorTypes.render:
return new renderable();
break;
}
}
That is two explicit function calls and an object boxing/unboxing operation! This operation is too expensive just to bounds check an enum. Does anyone know a cheaper alternative?
A pretty standard trick is to add members to the enum declaration that specify the first and last value:
Now it is a superfast test, takes about a nanosecond:
Do note that your switch statement already eliminates the need for this check.