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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:21:07+00:00 2026-06-17T13:21:07+00:00

I am working with some old C# code at the moment, which basically uses

  • 0

I am working with some old C# code at the moment, which basically uses derived types for the sole purpose of using the Type as a ‘property’, such as:

public abstract class Fruit
{
    public int Property { get; set; }
}

public class Apple : Fruit {}

public class Pear : Fruit {}

And then:

public void Foo(Fruit item)
{
    if(item is Apple)
    {
        // do something
        return;
    }
    if(item is Pear)
    {
        // do something
        return;
    }

    throw new ArgumentOutOfRangeException("item");
}

I would have included an enum property on BaseClass to specify the ‘type’:

public class Fruit
{
    public int Property { get; set; }

    public FruitType Type { get; set; }
}

public enum FruitType
{
    Apple,
    Pear
}

and then used it thus:

public void Foo(Fruit item)
{
    switch(item.Type)
    {
        case FruitType.Apple:
            // do something
            break;
        case FruitType.Pear:
            // do something
            break;
        default:
            throw new ArgumentOutOfRangeException();
    }
}

I feel that the former pattern is a misuse of inheritence, but are there any advantages to it that I should consider before re-writing this code?

  • 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-06-17T13:21:08+00:00Added an answer on June 17, 2026 at 1:21 pm

    The standard “OO” way to deal with this situation is to make DoSomething an abstract method on Fruit. Then the caller just calls DoSomething, knowing that the implementation will do the right thing.

    The downside of this approach is that it puts responsibility for working out all possible “somethings” that a user might possibly want onto the author of the abstract class.

    That downside can be mitigated by using the “visitor pattern”. The visitor pattern is a standard way to enable third parties to efficiently switch behaviours based on the runtime type of a value. You might consider researching it.

    Your second approach — discriminating the type with a tag — is quite common and can be very efficient. Roslyn uses this technique extensively. It is considered by OO purists to be a bit smelly, but fortunately I am not an OO purist.

    A variation on your second technique that I like is:

    public enum FruitKind { Apple, Orange }
    public abstract class Fruit
    {
      private Fruit(FruitKind kind)
      {
          this.Kind = kind;
      }
      public FruitKind Kind { get; protected set; }
      private class Apple : Fruit
      {
          public Apple() : base(FruitKind.Apple) {}
      }
      public static Fruit MakeApple() { return new Apple(); }
      // similarly for orange
    }
    

    Now the only way that a Fruit user can determine the type is through the tag, because Apple and Orange are not accessible. You know that no third party is going to make their own Fruit because the only Fruit constructor is private.

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

Sidebar

Related Questions

I am trying to change some old code which uses onclick so that I
I am working on some old code using ADODB in PHP . Queries look
I am working on editing some old C++ code that uses global arrays defined
I recently came upon some old code which I didn't write. It uses WWW::Facebook::API
I'm working some code that inserts csv rows into an SQLite database using Python.
I'm working on some old code and I found that I used to use
I'm working with some old VB6 code and have a scenario I'm trying to
I'm working on bringing some old code from 1998 up to the 21st century.
I have some working code that uses a SQL select statement within it. As
I'm working with some old code where major refactoring isn't an option. The following

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.