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

  • Home
  • SEARCH
  • 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 6672053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:27:46+00:00 2026-05-26T03:27:46+00:00

In the comments of this answer it is stated that checking whether the object

  • 0

In the comments of this answer it is stated that “checking whether the object has implemented the interface , rampant as it may be, is a bad thing“

Below is what I believe is an example of this practice:

public interface IFoo
{
   void Bar();
}

public void DoSomething(IEnumerable<object> things)
{
   foreach(var o in things)
   {
       if(o is IFoo)
           ((IFoo)o).Bar();
   }
}

With my curiosity piqued as someone who has used variations of this pattern before, I searched for a good example or explanation of why it is a bad thing and was unable to find one.

While it is very possible that I misunderstood the comment, can someone provide me with an example or link to better explain the comment?

  • 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-26T03:27:47+00:00Added an answer on May 26, 2026 at 3:27 am

    It depends on what you’re trying to do. Sometimes it can be appropriate – examples could include:

    • LINQ to Objects, where it’s used to optimize operations like Count which can be performed more efficiently on an IList<T> via the specialized members.
    • LINQ to XML, where it’s used to provide a really friendly API which accepts a wide range of types, iterating over values where appropriate
    • If you wanted to find all the controls of a certain type under a particular control in Windows Forms, you would want to check whether each control was a container to determine whether or not to recurse.

    In other cases it’s less appropriate and you should consider whether you can change the parameter type instead. It’s definitely a “smell” – normally you shouldn’t concern yourself with the implementation details of whatever has been handed to you; you should just use the API provided by the declared parameter type. This is also known as a violation of the Liskov Substitution Principle.

    Whatever the dogmatic developers around may say, there are times when you simply do want to check an object’s execution time type. It’s hard to override object.Equals(object) correctly without using is/as/GetType, for example 🙂 It’s not always a bad thing, but it should always make you consider whether there’s a better approach. Use sparingly, only where it’s genuinely the most appropriate design.


    I would personally rather write the code you’ve shown like this, mind you:

    public void DoSomething(IEnumerable<object> things)
    {
        foreach(var foo in things.OfType<IFoo>())
        {
            foo.Bar();
        }
    }
    

    It accomplishes the same thing, but in a neater way 🙂

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

Sidebar

Related Questions

This question arose from the discussion in the comments of this answer . First,
As a followon to the discussion in the comments of this answer , should
I read this answer and its comments and I'm curious: Are there any reasons
The comment to this answer got me wondering. I've always thought that C was
In a comment on this answer to another question , someone said that they
I am writing to clarify some comments on this website. 1) I know that
It has been asked before, but without a full answer . This is to
I'm working with existing code that has an object store in the form of
I came across a comment this answer which states that a library project: cannot
I got a comment to my answer on this thread: Malloc inside a function

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.