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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:01:50+00:00 2026-06-08T03:01:50+00:00

Just curious if there is a solution for this without making the base class

  • 0

Just curious if there is a solution for this without making the base class method virtual and the derived method marked override:

I want it to jump into the derived method, set the property to true (for testing purposes, almost like this is a mock), but it’s jumping into the base class. The only solution is to make the base virtual, I thought I could mark it new in the derived and that would work.

   public class SomeClass
   {
      private readonly IFoo _foo;

      public SomeClass(IFoo foo)
      {
         _foo = foo;
      }

      public void DoSomething()
      {
         _foo.DoFoo();
      }
   }

   public interface IFoo
   {
      void DoFoo();
   }

   public class Foo : IFoo
   {
      public void DoFoo()
      {

      }
   }

   public class Foo2 : Foo
   {
      public bool DoFooWasCalled { get; set; }

      public new void DoFoo()
      {
         DoFooWasCalled = true;
         base.DoFoo();
      }
   }

Here’s the test that is failing:

   [TestFixture]
   public class TestingCSInheritance
   {
      [Test]
      public void TestMethod()
      {
         var foo = new Foo2();
         var someClass = new SomeClass(foo);
         someClass.DoSomething();
         foo.DoFooWasCalled.ShouldBe(true);
      }
   }

My guess is that because I’m using dependency injection with an interface it’s using the base class, but I’m not sure why.

  • 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-08T03:01:53+00:00Added an answer on June 8, 2026 at 3:01 am

    The only solution is to make the base virtual, I thought I could mark it new in the derived and that would work.

    No. If you create a new method, only code which knows about the expression with a compile-time type of Foo2 will call Foo2.DoSomething.

    Even though TestMethod knows about foo as a Foo2, SomeClass.DoSomething only knows about it as IFoo, so it’s just using the IFoo implementation. Foo2.DoSomething is not an implementation of IFoo.

    You want polymorphism, but you’ve effectively discarded it by not using a virtual method.

    The only other option is to reimplement IFoo in Foo2, which is the solution described by Sergey. Note that this ends up with some pretty confusing code. Method hiding of any kind almost always does. Fundamentally, if you want the effect of a virtual method, you should use a virtual method…

    Alternatively, it would be cleaner if you used composition instead of inheritance:

    public class MockFoo : IFoo
    {
        private readonly Foo foo = new Foo();
        private bool doSomethingWasCalled;
    
        public void DoSomething()
        {
            doSomethingWasCalled = true;
            foo.DoSomething();
        }
    }
    

    This delegates the call to DoSomething instead of trying to juggle an inheritance hierarchy.

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

Sidebar

Related Questions

Fairly simple; I'm just curious if there's a better way to accomplish this. Given
Just curious if there is anyway to display an objects retain count using NSLog.
I'm just curious if there's a reason why in order to represent -1 in
I am just curious if there is a way to swap the two stings
I am just curious is there any way to determine if a particular module
I'm just curious if there is any correlation between the length of the address
Just curious, is there a format string I can use to output something like
I'm just curious, but is there a name for the process using print statements
Just a curious question but is there any programs that can help/aid you when
I'm guessing the answer is no, but just in case, I'm curious if there's

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.