0

Duplicate:

Is it possible to make a parameter implement two interfaces?


Looking for a way to do this :

public interface INumber1 {}
public interface INumber2 {}
public class TestObject : INumber1, INumber2 {}

public static class TestClass
{
    public static void TestMethod(INumber1&INumber2 testobject)
    {
        ...
    }
}

Calling the TestMethod :

TestClass.TestMethod(new TestObject());

The thing is that I need a parameter to contain methods from both INumber1 and INumber2. Tips?

Community
  • 1
  • 1
Matteus Hemström
  • 3,796
  • 2
  • 26
  • 34
  • 1
    Exact Duplicate From Earlier Today http://stackoverflow.com/questions/772053/is-it-possible-to-make-a-parameter-implement-two-interfaces – Eoin Campbell Apr 21 '09 at 14:58

4 Answers4

5

Try this one:

public static void TestMethod<T>(T testObject) Where T:INumber,INumber2
{

}
Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
Jose Basilio
  • 50,714
  • 13
  • 121
  • 117
0

Create a common interface.

leppie
  • 115,091
  • 17
  • 196
  • 297
0

You could use a generic method:

public static void TestMethod<T>(T testObject)
    where T : INumber1, INumber2
{
}

Alternatively you could create an interface which extends both INumber1 and INumber2:

public interface INumber1And2 {}

public class TestObject : INumber1And2

Personally I prefer the generic solution if you can get away with it, but sometimes generics can complicate things significantly. On the other hand, you manage to keep the two interfaces nice and isolated.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
-1

Is it possible to have Inumber2 inherit from Inumber1? If not, would making a generic method work?

public static void TestMethod<T>(T testObject);
Mike_G
  • 16,237
  • 14
  • 70
  • 101