Is there a built-in FxCop rule that analyses for unnecessary Boxing/UnBoxing?
The following example shows a method, WeaklyTyped, which violates the rule and a method, StronglyTyped, which satisfies the rule.
using System;
namespace PerformanceLibrary
{
public interface IWork
{
object DoWork();
}
public class Work : IWork
{
object IWork.DoWork()
{
return 3;
}
public int DoWork()
{
return 3;
}
}
public class NeedsWork
{
public void WeaklyTyped()
{
IWork iwork = new Work();
// The following call violates the rule.
int x = (int)iwork.DoWork();
}
public void StronglyTyped()
{
Work work = new Work();
int x = work.DoWork();
}
}
}