0

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();
        }
    }
} 
Ray Hayes
  • 14,896
  • 8
  • 53
  • 78
Vivin joy
  • 97
  • 7

1 Answers1

0

There used to be a rule for that but that is no longer in the current build of FxCop.

You can use the tool BoxCop but that does not integrate nicely into the build process.

caldis
  • 226
  • 1
  • 9