0
  1. Say I have an interface IFoo with a method FooMethod() and a class Foo : IFoo.
  2. Say I have another interface IBar with method BarMethod() and a class Bar : IBar.
  3. Now I can create an object Foo obj1 = Foo() and call obj1.FooMethod().
  4. I can also create an object Bar obj2 = Bar() and call obj2.Bar().
  • I can now make a method that accepts e.g. an object of IFoo, e.g. obj1, such that
void FooExample(IFoo fooObj) 
  => fooObj.FooMethod();

FooExample(obj1);  // Because typeof(obj1) == Foo and Foo : IFoo
  • Can I somehow now make a method:
void CombinedExample((IFoo, IBar)foobarObj)
{
  foobarObj.Foo();
  foobarObj.Bar();
}

I know I can make a new (empty) interface IFooBar: IFoo, IBar such that I can now change the type of the parameter of CominedExample can be changed to IFooBar foobarObj, but I am particularly curious if there is a way of combining two (or even more) interfaces / classes to create a temporary object that inherits from these types without having to create a new empty type that just exists to combine those other types which I might only need in very few occasions.

Any suggestion to solve this 'problem' are welcome!

14
  • 1. Say I have an interface IFoo with a method Foo() then I am able to create an object IFoo obj such that I can do: obj.Foo(); No. In C#, you can only create object instances from classes, not from interfaces. Commented Jul 14, 2022 at 17:02
  • It sounds like you want to create a variable that can only hold objects implementing both IFoo and IBar. In C#, that is (only) possible with generics: stackoverflow.com/q/2755948/87698. Commented Jul 14, 2022 at 17:04
  • 1
    class FooBar : IFoo, IBar { ... } Commented Jul 14, 2022 at 17:10
  • @Heinzi, the problem with Sith-like statements like "In C#, you can only create object instances from classes, not from interfaces" (ie, absolute statements) is that they're generally wrong for a small subset of cases. For example, it is perfectly legal in a small subset of cases to call new on an interface and get back an object in C#. Commented Jul 14, 2022 at 17:12
  • 1
    @Blindy: I honestly did not know that. Can you give me an example of new ISomeInterface working in C#? I thought that was only possible in Java... Commented Jul 14, 2022 at 17:40

3 Answers 3

1

You can leverage generics with constraints to achieve what you need. You can create a method like this:

public void DoFooBar<T>(T fooBar)
    where T : IFoo, IBar
{
    fooBar.CallFooMethod();
    fooBar.CallBarMethod();
}
Sign up to request clarification or add additional context in comments.

Comments

-1

then I am able to create an object IFoo obj [...]

You can create a variable with the interface as its type, sure, but that's not an object. It's an empty variable sitting there with literally null in it.

[...] such that I can do: obj.Foo();

You can write that, but it will crash at run time with a null reference exception. See above.

to create a temporary object that inherits

I particularly want to highlight that quote because it's so utterly ridiculous that it should be the starting point to fixing whatever's broken in your understanding.

Anyway, yes, you can have classes inherit multiple interfaces and then create instances (objects as you call them) of those classes and call the interface methods as you wish. But that's (probably?) what you meant. What you wrote is utter gibberish.

1 Comment

I rephrased my question
-2

As far as I know, one of the main advantages of interfaces is that you can implement more that one interface in a class. Here is an example from codeguru:

 4:  using System;
 5:
 6:  public interface IShape
 7: {
 8:     // Cut out other methods to simplify example.
 9:     double Area();
10:     int Sides { get; }
11:  }
12:
13:  public interface IShapeDisplay
14:  {
15:     void Display();
16:  }
17:
18:  public class Square : IShape, IShapeDisplay
19:  {
20:     private int InSides;
21:     public  int SideLength;
22:
23:     public int Sides
24:     {
25:        get { return InSides; }
26:     }
27:
28:     public double Area()
29:     {
30:        return ((double) (SideLength * SideLength));
31:     }
32:
33:     public double Circumference()
34:     {
35:        return ((double) (Sides * SideLength));
36:     }
37:
38:     public Square()
39:     {
40:        InSides = 4;
41:     }
42:
43:     public void Display()
44:     {
45:        Console.WriteLine("nDisplaying Square information:");
46:        Console.WriteLine("Side length: {0}", this.SideLength);
47:        Console.WriteLine("Sides: {0}", this.Sides);
48:        Console.WriteLine("Area: {0}", this.Area());
49:     }
50:  }
51:
52:  public class Multi
53:  {
54:     public static void Main()
55:     {
56:        Square mySquare = new Square();
57:        mySquare.SideLength = 7;
58:
59:        mySquare.Display();
60:     }
61:  }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.