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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:08:51+00:00 2026-06-09T01:08:51+00:00

I have a base class like this: class FooBase { public bool Do(int p)

  • 0

I have a base class like this:

class FooBase
{
    public bool Do(int p) { /* Return stuff. */ }
}

And a child class like this:

class Foo<T> : FooBase
{
    private Dictionary<T, int> Dictionary;

    public bool Do(T p)
    {
        int param;
        if (!Dictionary.TryGetValue(p, out param))
            return false;
        return base.Do(param);
    }
}

If the user creates a Foo<string> object called “fooString”, then he can call both fooString.Do(5) and fooString.Do("test") but if he creates a Foo<int> object called “fooInt”, he can only call the Do method of the derived class. I prefer the second no matter what the T is.

The Do methods in both of these classes essentially do the same thing. The one in the derived class gets an integer from a Dictionary<T, int> using the given parameter and calls the Do method of the base class using it.

That’s why I want to hide the Do method of the FooBase in Foo<T>. How can I achieve this or something similar? Any design advice to overcome this would also be nice.

  • 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-09T01:08:53+00:00Added an answer on June 9, 2026 at 1:08 am

    but if he creates a Foo<int> object called “fooInt”, he can only call the Do method of the derived class.

    No, that’s not true. If the declared type of the variable is FooBase, it will still call the FooBase method. You’re not really preventing access to FooBase.Do – you’re just hiding it.

    FooBase foo = new Foo<int>();
    foo.Do(5); // This will still call FooBase.Do
    

    Full sample code to show that:

    using System;
    
    class FooBase
    {
        public bool Do(int p) { return false; }
    }
    
    class Foo<T> : FooBase
    {
        public bool Do(T p) { return true; }
    }
    
    class Test
    {
        static void Main()
        {
            FooBase foo1 = new Foo<int>();
            Console.WriteLine(foo1.Do(10)); // False
    
            Foo<int> foo2 = new Foo<int>();
            Console.WriteLine(foo2.Do(10)); // True
        }
    }
    

    That’s why I want to hide the Do method of the FooBase in Foo.

    You need to think about Liskov’s Substitutability Principle.

    Either Foo<T> shouldn’t derive from FooBase (use composition instead of inheritance) or FooBase.Do shouldn’t be visible (e.g. make it protected).

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

Sidebar

Related Questions

I have a base class like this: public class Trajectory{ public int Count {
I have a piece of code like this class Base { public: Base(bool _active)
I have a base class like this: public class BaseResponse { public string ErrorMessage
I have a base class which looks something like this: class Base { public:
I have an abstract entity base class defined like this: public abstract class SessionItem
I have a base class for ado.net operations, that looks like this: public abstract
I have a base class like this: public class BaseModalCommand { protected object m_commandArgument;
I have a base class looking like this: public class BaseController : Controller {
Suppose I have this class hierarchy: class A { public: virtual void foo(Base *b)
I have a base class like this class Base { public var space:Number; }

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.