Tanya
Tanya

Reputation: 1621

Generic method for 2 generic list?

I have 2 Lists List<Class1> and List<Class2>. I need to create a Method which will do some manupulations. how to create a Generic Method for passing List as Parameter.

Void ManipulateList(IList<obj> list)
{
    //statement;
}

Upvotes: 1

Views: 109

Answers (2)

Ash Burlaczenko
Ash Burlaczenko

Reputation: 25435

Using generics is one option as suggested by SLaks. You could also use an Interface and have both Class1 and Class2 implement it, e.g.

public interface IClass { ... }

public class Class1 : IClass { ... }

public class Class2 : IClass { ... }

Then you method would be

void ManipulateList(IList<IClass> list)

Upvotes: 2

SLaks
SLaks

Reputation: 887275

You're trying to create a generic method:

public void ManipulateList<T>(IList<T> list)

If you want to be able to do things with the items in the lists, you'll probably want to add a generic constraint.

Upvotes: 6

Related Questions