tags:

views:

77

answers:

5

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?

A: 

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
A: 

Create a common interface.

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

}
Jose Basilio
A: 

Duplicate.

Pontus Gagge
A: 

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