© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
8 replies
Binto86

✅ ✅ Use function that has default implementation in interface from instance of class

I have interface similar to this:
interface IFoo
{
  void DoThing1();
  void DoThing2();

  void DoMultipleThings(int thing1, int thing2)
  {
    for (int i = 0; i < thing1; i++)
    {
      this.DoThing1();
      for (int j = 0; j < thing2; j++)
      {
        this.DoThing2();
      }
    }
  }
}
interface IFoo
{
  void DoThing1();
  void DoThing2();

  void DoMultipleThings(int thing1, int thing2)
  {
    for (int i = 0; i < thing1; i++)
    {
      this.DoThing1();
      for (int j = 0; j < thing2; j++)
      {
        this.DoThing2();
      }
    }
  }
}

now i have class like this:
class Bar : IFoo
{
  void DoThing1() => //something
  void DoThing2() => //something
}
class Bar : IFoo
{
  void DoThing1() => //something
  void DoThing2() => //something
}

and i want to use it like this:
Bar bar = new Bar();
bar.DoMultipleThings(5, 5);
Bar bar = new Bar();
bar.DoMultipleThings(5, 5);

is this possible? maybe some keyword on the interface or something (i know i can cast it, but i don't want to do that)
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ abstract class virtual methods vs default implementation interface
C#CC# / help
4y ago
❔ Default implementation of a `static abstract` base interface member in derived interface
C#CC# / help
3y ago
❔ Make interface deriving from another interface have a sealed implementation of a function
C#CC# / help
3y ago
Using a default implementation on an interface
C#CC# / help
2y ago