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 819755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:21:47+00:00 2026-05-15T02:21:47+00:00

I’m thinking about designing a method that would return an object that implements an

  • 0

I’m thinking about designing a method that would return an object that implements an interface but whose concrete type won’t be know until run-time. For example suppose:

ICar
Ford implements ICar
Bmw implements ICar
Toyota implements ICar

public ICar GetCarByPerson(int personId)

We don’t know what car we will get back until runtime.

a) I want to know what type of car the person has.

b) depending on the concrete car type we get back we will call different methods (because some methods only make sense on the class). So the client code will do something like.

ICar car = GetCarByPerson(personId);

if ( car is Bmw )
{
  ((Bmw)car).BmwSpecificMethod();
}
else if (car is Toyota)
{
  ((Toyota)car).ToyotaSpecificMethod();
}

Is this a good design? Is there a code smell? Is there a better way to do this?

I’m fine with the method that returns the interface, and if the client code was calling interface methods obviously this would be fine. But my concern is whether the client code casting to concrete types is good design.

  • 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-15T02:21:47+00:00Added an answer on May 15, 2026 at 2:21 am

    Using the is keyword in C# (in the manner you have demonstrated above) is almost always a code smell. And it stinks.

    The problem is that something which is supposed to only know about an ICar is now required to keep track of several different classes that implement ICar. While this works (as in it produces code that operates), it’s poor design. You’re going to start off with just a couple cars…

    class Driver
    {
        private ICar car = GetCarFromGarage();
    
        public void FloorIt()
        {
            if (this.car is Bmw)
            {
                ((Bmw)this.car).AccelerateReallyFast();
            }
            else if (this.car is Toyota)
            {
                ((Toyota)this.car).StickAccelerator();
            }
            else
            {
                this.car.Go();
            }
        }
    }
    

    And later on, another car is going to do something special when you FloorIt. And you’ll add that feature to Driver, and you’ll think about the other special cases that need to be handled, and you’ll waste twenty minutes tracking down every place that there is a if (car is Foo), since it’s scattered all over the code base now — inside Driver, inside Garage, inside ParkingLot… (I’m speaking from experience in working on legacy code here.)

    When you find yourself making a statement like if (instance is SomeObject), stop and ask yourself why this special behavior needs to be handled here. Most of the time, it can be a new method in the interface/abstract class, and you can simply provide a default implementation for the classes that aren’t “special”.

    That’s not to say that you should absolutely never check types with is; however, you must be very careful in this practice because it has a tendency to get out of hand and become abused unless kept in check.


    Now, suppose you have determined that you conclusively must type-check your ICar. The problem with using is is that static code analysis tools will warn you about casting twice, when you do

    if (car is Bmw)
    {
       ((Bmw)car).ShiftLanesWithoutATurnSignal();
    }
    

    The performance hit is probably negligible unless it’s in an inner loop, but the preferred way of writing this is

    var bmw = car as Bmw;
    if (bmw != null) // careful about overloaded == here
    {
        bmw.ParkInThreeSpotsAtOnce();
    }
    

    This requires only one cast (internally) instead of two.

    If you don’t want to go that route, another clean approach is to simply use an enumeration:

    enum CarType
    {
        Bmw,
        Toyota,
        Kia
    }
    
    interface ICar
    {
        void Go();
    
        CarType Make
        {
            get;
        }
    }
    

    followed by

    if (car.Make == CarType.Kia)
    {
       ((Kia)car).TalkOnCellPhoneAndGoFifteenUnderSpeedLimit();
    }
    

    You can quickly switch on an enum, and it lets you know (to some extent) the concrete limit of what cars might be used.

    One downside to using an enum is that CarType is set in stone; if another (external) assembly depends on ICar and they added the new Tesla car, they won’t be able to add a Tesla type to CarType. Enums also don’t lend themselves well to class hierarchies: if you want a Chevy to be a CarType.Chevy and a CarType.GM, you either have to use the enum as flags (ugly in this case) or make sure you check for Chevy before GM, or have lots of ||s in your checks against the enums.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There are a couple of plugins for Firefox out there… May 15, 2026 at 12:57 pm
  • Editorial Team
    Editorial Team added an answer The function you are looking for is called LEAST(): select… May 15, 2026 at 12:57 pm
  • Editorial Team
    Editorial Team added an answer You can also use JSONP by adding callback=? to the… May 15, 2026 at 12:56 pm

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.