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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:28:51+00:00 2026-06-17T18:28:51+00:00

Firstly I am pretty new to C#. I would like to have an interface

  • 0

Firstly I am pretty new to C#. I would like to have an interface declare a member function like in the following piece of code

interface IMyInterface {
    void MyAction() {
        // do stuff depending on the output of function()
    }
    void Function();
}

here Function is pure virtual and should be implemented by children of IMyInterface. I could use an abstract class instead of an interface but then I could not inherit from other classes… Say for example that MyAction is recursiverly searching a directory for files and applying Function to any file found to make my example clear.

How to change my design in order to overcome the constraint that interfaces cannot implement classes ?

Edit : In C++ what I would do is using templates as such

template<class A>
static void MyAction(const A& a) {
    // do stuff depending on the output of A::Function()
};

class MyClass {
    void Function();
};

I was wondering if there were an elegant way to do this using interfaces in C#.

  • 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-17T18:28:52+00:00Added an answer on June 17, 2026 at 6:28 pm

    In C# you don’t have multiple inheritance. You can circumvent this limitation by using composition.

    Define your interface like this (Function needs not to be defined here):

    public interface IMyInterface
    {
        void MyAction();
    }
    

    Declare an abstract class with an abstract Function and implementing this interface:

    public abstract class MyInterfaceBase : IMyInterface
    {
        public void MyAction()
        {
            // Do stuff depending on the output of Function().
            Function();
        }
    
        protected abstract void Function();
    }
    

    From this abstract class you can derive a concrete implementation. This is not yet your “final” class, but it will be used to compose it.

    public class ConcreteMyInterface : MyInterfaceBase
    {
        protected override void Function()
        {
            Console.WriteLine("hello");
        }
    }
    

    Now let’s come to your “final”, composed class. It will derive from SomeBaseClass and implement IMyInterface by integrating the functionality of ConcreteMyInterface:

    public class SomeBaseClass
    {
    }
    
    public class MyComposedClass : SomeBaseClass, IMyInterface
    {
        private readonly IMyInterface _myInterface = new ConcreteMyInterface();
    
        public void MyAction()
        {
            _myInterface.MyAction();
        }
    }
    

    UPDATE

    In C# you can declare local classes. This comes even closer to multiple inheritance, as you can derive everything within your composing class.

    public class MyComposedClass : SomeBaseClass, IMyInterface
    {
        private readonly IMyInterface _myInterface = new ConcreteMyInterface();
    
        public void MyAction()
        {
            _myInterface.MyAction();
        }
    
        private class ConcreteMyInterface : MyInterfaceBase
        {
            protected override void Function()
            {
                Console.WriteLine("hello");
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Firstly, I'd like to show you a piece of code. The code is a
Firstly, I wrote my customized setter for an NSString* like this: - (void)setDateString:(NSString *)newDateString
I'm pretty new to databases and sql. I have a problem where I have
I'm pretty new to coding for iOS and i have a question about setting
Ok, i'd like to firstly make you notice that i have looked for php
Firstly, I'm pretty new to C++. I believe that getline() isn't a standard C
I have what I would assume is a pretty standard setup...spring, jpa (hibernate) and
Firstly, I had a snippet as following: struct D { int sum; D():sum(0){accum();} void
Firstly, let me set out what I'd like to do. Assume I have three
Firstly, this seems like something that should have been asked before, but I cannot

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.