I'm working on some project and I have situation where I need to create a list that would accept classes which implement at least one interface
.
It should look like this:
interface A
{
void foo();
}
interface B
{
void bar();
}
class C : A, B
{
public void foo()
{
// Do some stuff specific for A
}
public void bar()
{
// Do some other stuff for B
}
}
class D : B
{
public void bar()
{
// Do some actions for B
}
}
class E : A
{
public void foo()
{
// Do something specific for A
}
}
class Program
{
static void Main(string[] args)
{
List<A+B> list = new List<A+B>();
list.Add(new C());
list.Add(new D());
list.Add(new E());
foreach (A a in list)
{
a.foo();
}
foreach (B b in list)
{
b.bar();
}
}
}
Edit:
List<object>
doesn't really fits because I need to hold classes with interfaces A and B and execute specific logic for each interface
.