C# - Multi-interface field

Started by
0 comments, last by Telastyn 15 years, 3 months ago
Let's say I have two interfaces, IOne and ITwo, and I want some other class to have a class property that subscribes to both. Is the best approach: 1.

public class MyClass<T> where T : IOne, ITwo
{
   public T MyProperty { get; set; }
}

2.

interface IOneAndTwo : IOne, ITwo
{
}

public class MyClass
{
   public IOneAndTwo MyProperty  { get; set; }
}

3. Something else. I'm not positive that #2 will work, but I think so. The idea is to accomplish something like the following:

public class MyOtherClass : IOne, ITwo
{
// stuff
}

MyClass myClass = new MyClass();
MyOtherClass myOtherClass = new MyOtherClass();
myClass.MyProperty = myOtherClass

Thoughts?
Without order nothing can exist - without chaos nothing can evolve.
Advertisement
In plain C#, a class must inherit from both interfaces to be considered an instance that satisfies them. If you want to inherit directly from the interfaces or create a third interface that inherits from the two (and is then used/inherited by others), that's a design choice that depends on the usage.

The generic should work like that too, but again depends on the usage.

This topic is closed to new replies.

Advertisement