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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:36:44+00:00 2026-06-07T13:36:44+00:00

So say I had a C# class, class Foo : Bar, IBar, IBar2 {

  • 0

So say I had a C# class,

class Foo : Bar, IBar, IBar2
{

}

… where Bar is a class, and IWhatever and IFine are interfaces. I plan to use a similar implementation of IWhatever and IFine across multiple classes- the only sane way I can see to encapsulate this code and reuse it across all the classes is to make a class Whatever that inherits from IWhatever and Fine that inherits from IFine and make them members of the class that implements those interfaces and then call their members in the members implemented from the interfaces, like so:

class Foo : Bar, IWhatever, IFine
{
   IWhatever mWhatever;
   IFine mFine;
   Foo()
   {
      mWhatever = new Whatever();
      mFine = new Fine();
   }
   // from IWhatever
   void Kick()
   {
      mWhatever.Kick();
   }
   // from IFine
   void Punch()
   {
      mFine.Punch();
   }
}

Am I doing it right? Is there a better way?

  • 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-07T13:36:45+00:00Added an answer on June 7, 2026 at 1:36 pm

    When you are trying to implement the same multiple implementations across many classes via inheritance, that is a design smell. C#’s lack of multiple (class) inheritance is actually an asset, not a liability in this regard, because it enforces the single-responsibility principle.

    Think of it this way: the two types of abstractions available in C# have two complementary forms of representing real-life entities.

    • Abstract class: “Is a”
    • Interface: “Does something” or “Has such and such qualities/behavior”

    The implication of this is that interfaces should almost always be “hollow” because we assume that even though different classes may implement the same interface, it is likely that each one will do it differently.

    Be careful about the use of inheritance. Remember the sage wisdom of the GOF:

    Favor object composition over inheritance.

    So getting back to the problem you’re trying to solve, it might be better to do this:

    public class Foo : Bar
    {
       public IWhatever Whatever
       {
          get;
          private set;
       }
    
       public IFine Fine
       {
          get;
          private set;
       }
    
       public Foo(IWhatever whatever, IFine fine)
       {
          Whatever = whatever;
          Fine = fine;
       }
    }
    

    Now use an IOC container to inject concrete implementations of those interfaces. This is my humble recommendation.

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

Sidebar

Related Questions

Say I had class/struct Foo struct Foo { int a, b; bool operator< (Foo
Say I had a class SuperClass and two subclasses SubClassA and SubClassB that inherit
This is probably a very simple question. Say I had a class that computed
Say I had a very simple struct in c# public struct foo{ public int
Let's say I have models that look like this: class Foo < ActiveRecord::Base has_many
If you had a single class you needed to import, say java.util.Random, would naming
Let's say I had a class like this: class Parser attr_accessor :config, :html def
Say I had a class: public class Post { public int PostId { get;
Let's say I have a class called 'foo' in namespace abc... namespace abc {
Say I had the two tables below, how do I find all questions with

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.