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

Ask A Question

Stats

  • Questions 396k
  • Answers 396k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Although the solution below only uses the Python Win32 extensions… May 15, 2026 at 2:54 am
  • Editorial Team
    Editorial Team added an answer If the check boxes are dynamically created this is a… May 15, 2026 at 2:54 am
  • Editorial Team
    Editorial Team added an answer When you build asterisk, before the plain "make" you have… May 15, 2026 at 2:54 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.