Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 550467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:17:29+00:00 2026-05-13T11:17:29+00:00

First, I was trying to do something like : class Class1 { public void

  • 0

First,
I was trying to do something like :

class Class1
{
    public void Do()
    {
    }
}
class Class2
{
    public void Do()
    {
    }
}

…

if (o is Class1)
{
    Class1 c = (Class1)o;
    c.Do();
}
if (o is Class2)
{
    Class2 c = (Class2)o;
    c.Do();
}

but fxcop tells me that :
Performance Rules, Avoid duplicate casts where possible, since there is a cost associated with them. Cache the
result of the ‘as’ operator …

So I’ve done :

Class1 c1 = o as Class1;
Class2 c2 = o as Class2;
if (c1 != null)
    c1.Do();
if (c2 != null)
    c2.Do();

and no more fxcop error.
But I’ve try to measure if it’s really a good Performance rule with :

static void Main()
    {
        object o = new Class1();
        int cst = 100000000;

        Stopwatch sw1 = new Stopwatch();
        Stopwatch sw2 = new Stopwatch();
        sw1.Start();
        for (int i = 0; i < cst; i++)
        {
            GoodPerf(o);
        }
        sw1.Stop();
        var t1 = sw1.ElapsedMilliseconds;

        sw2.Start();
        for (int i = 0; i < cst; i++)
        {
            BadPerf(o);
        }
        sw2.Stop();
        var t2 = sw2.ElapsedMilliseconds;
        Console.WriteLine(t1);
        Console.WriteLine(t2);
    }

    private static void BadPerf(object o)
    {
        if (o is Class1)
        {
            Class1 c = (Class1)o;
            c.Do();
        }
        if (o is Class2)
        {
            Class2 c = (Class2)o;
            c.Do();
        }
    }

    private static void GoodPerf(object o)
    {
        Class1 c1 = o as Class1;
        Class2 c2 = o as Class2;
        if (c1 != null)
            c1.Do();
        if (c2 != null)
            c2.Do();
    }

And it displays :

2090
1725

2090 is supposed to be the good perf …

So … I’m asking if I really should to follow this rule …
Maybe, I’m not correcting the good way…

Thanks for your answer

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:17:30+00:00Added an answer on May 13, 2026 at 11:17 am

    FxCop isn’t always right, it’s oversimplifying sometimes and this is one of the most common scenarios. Unless you’re casting 3 or 4+ times, you’re not going to outweigh the cost of the as operator…but the FxCop rule doesn’t check like this, it just does >1

    Maybe it will be better check with 4.0 VS release? I assume since dynamic is out there, they’ve had to revisit a lot of these rules, but we won’t know until it ships probably. The beta 2 I have still currently checks in the style you posted, giving some bad advice in this case IMO.

    However, keep in mind that this is minuscule in terms of performance, not something to worry about unless you’re looping hundreds of thousands of times.

    Something else to keep in mind, if you look at the IL then part of the time it in-lines the damn thing anyway, making the warning completely useless.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write my first real python function that does something real.
first of all i'm kinda new to C# I'm trying to do something like
I'm currently trying to implement a StopWatch class. The interface is something like: interface
I'm trying out my first Spring project and must be doing something really stupid
Trying to create my first iPhone app that would play back audio. When I
First, context: I'm trying to create a command-line-based tool (Linux) that requires login. Accounts
I'm trying out Linq for the first time and having a bit of difficult
I'm trying to build my first generic list and have run into some problems.
I'm trying to make my first web service client for the eBay API using
I'm trying to implement an outdent of the first letter of the first paragraph

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.