I get the following error when running FXCop:
CA1800 : Microsoft.Performance : 'obj', a variable, is cast to type 'Job' multiple times in method 'ProductsController.Details(int, int)'. Cache the result of the 'as' operator or direct cast in order to eliminate the redundant castclass instruction
Code:
object obj = repository.GetJobOrPlace(jobId);//Returns (object) place or (object) product
if (obj != null)
{
if (obj is Job)
{
Job j = (Job) obj;
Debug.WriteLine(j.Title);
}
else if (obj is Place)
{
Place p = (Place) obj;
Debug.WriteLine(p.Title);
}
}
What's wrong with this? I can only see one cast: Job j = (Job) obj.