jsmars
jsmars

Reputation: 1918

Multiple interfaces contained in one List<T>

Is there a way to define the type in a generic class like List to have contain objects which only implent multiple interfaces? Possibly class type and interfaces.

For example:

List<myObjectBase, IDisposable, IClonable> myList;

Upvotes: 0

Views: 4040

Answers (6)

Navid Madani
Navid Madani

Reputation: 11

Below is the simplest solution for adding multiple interfaces that worked for me.

List<ICommonInterface> myList = new List<ICommonInterface>()

myFirstClass m1C = new myFirstClass();
mySecondClass m2C = new mySecondClass();

myList.Add(m1C);
myList.Add(m2C);

foreach (var item in myList)
{
    item.Clone();
    item.Dispose();
}

class myFirstClass : ICommonInterface  
{  
// implement interface methods  
}  

class mySecondClass : ICommonInterface  
{  
// implement interface methods  
}  


interface ICommonInterface : IDisposable, IClonable  
{  
}  


interface IDisposable  
{  
    void Dispose(){}  
}  

interface IClonable     
{  
    void Clone(){}  
}  

Upvotes: 1

supercat
supercat

Reputation: 81115

One approach that may be helpful is to define an interface ISelf<out T> whose one member, Self, simply returns "this" as a T; then for any interface IWhatever that might be combined, define a generic version IWhatever<out T> which inherits both the IWhatever and ISelf<T>. In that case, a class Whizbang which implements IFoo<Whizbang> and IBar<Whizbang> will implicitly implement ISelf<Whizbang>, IFoo<IBar<Whizbang>>, IBar<IFoo<Whizbang>>, etc. A routine which needs something that implements both IFoo and IBar can accept a parameter of type IFoo<IBar>; that parameter will implement IFoo; its Self property will implement IBar. Any object which implements multiple interfaces using this pattern may be cast to a nested interface type of the given form using some or all of the interfaces, listed in any order.

Upvotes: 1

Aykut &#199;evik
Aykut &#199;evik

Reputation: 2088

You can use an ArrayList and can check the Type of an object in this list - maybe it is handier.

if(list[i] is Type)

Upvotes: 0

Roman Starkov
Roman Starkov

Reputation: 61382

Not sure if I understood correctly, but how about this:

class MyList<T> : List<T>
  where T : myObjectBase, IDisposable, IClonable
{
}

This way you can only add objects to the list which derive from the base and implement those interfaces.

Upvotes: 8

Justin Niessner
Justin Niessner

Reputation: 245389

No, multiple generic parameters are not supported.

It wouldn't make much sense either. There would be no benefit of using the generic List<T> class over something like an ArrayList. You would lose all of the type safety benefits and you'd wind up still having to cast things all over the place.

The better option would be to create a composite class that handles all of the things you want to do...and then use that:

public class CommonBase : MyBaseClass, ICloneable, IDisposable
{
}

And then use that as your generic parameter:

var newList = new List<CommonBase>();

Upvotes: 1

Achim
Achim

Reputation: 15692

No. In that case you would have to express that in the following way:

public class CommonStuff : MyObjectBase, IDisposable, IClonable {}

Then you can write:

List<CommonStuff> myList;

Upvotes: 1

Related Questions