How to declare function that accepts any generic list as input parameter?

by marss on 10/31/2007 6:29:00 PM How to declare function that accepts any generic list as input
parameter? I mean I need
void foo(List<> list)
{
}
where:
Valid parameters are List<SomeInfo>, List<string>, List<Guid>, e.t.c.
Invalid parameters are SomeInfo[], string[], Array, ArrayList,
Dictionary<int, string>, e.t.c

Is it possible?

TIA,
Mykola
http://marss.co.ua

 

Re: How to declare function that accepts any generic list as input parameter?

by Jon Skeet [C# MVP] on 10/31/2007 6:51:00 PM On Nov 1, 8:29 am, marss <marss...@gmail.com> wrote:
> How to declare function that accepts any generic list as input
> parameter? I mean I need
> void foo(List<> list)
> {}
>
> where:
> Valid parameters are List<SomeInfo>, List<string>, List<Guid>, e.t.c.
> Invalid parameters are SomeInfo[], string[], Array, ArrayList,
> Dictionary<int, string>, e.t.c
>
> Is it possible?

Yes - you need to make the method generic:

void Foo<T>(List<T> list)
{
}

Jon

 

Re: How to declare function that accepts any generic list as input parameter?

by marss on 10/31/2007 6:54:00 PM On 1 , 10:50, "Jon Skeet [C# MVP]" <sk...@pobox.com> wrote:
> On Nov 1, 8:29 am, marss <marss...@gmail.com> wrote:
>
> > How to declare function that accepts any generic list as input
> > parameter? I mean I need
> > void foo(List<> list)
> > {}
>
> > where:
> > Valid parameters are List<SomeInfo>, List<string>, List<Guid>, e.t.c.
> > Invalid parameters are SomeInfo[], string[], Array, ArrayList,
> > Dictionary<int, string>, e.t.c
>
> > Is it possible?
>
> Yes - you need to make the method generic:
>
> void Foo<T>(List<T> list)
> {
>
> }
>
> Jon

Thanks

 

Re: How to declare function that accepts any generic list as input parameter?

by Marc Gravell on 10/31/2007 6:58:00 PM > Valid parameters are List<SomeInfo>, List<string>, List<Guid>, e.t.c.
> Invalid parameters are SomeInfo[], string[], Array, ArrayList,
For info, if you want it to additionally work with arrays [i.e. T[],
not Array], (typed) collections, etc - then another option is to use
the less specific IList<T> interface:

void Foo<T>(IList<T> list)
{
}

This obviously won't give you as many methods etc as List<T>, but it
should allow most options. In .NET 3.5, extension methods (in
System.Linq) mean that IList<T> exposes almost as much functionality
as List<T> does (albeit via subtly different implementation).

Marc

 

Re: How to declare function that accepts any generic list as input parameter?

by marss on 11/1/2007 12:12:00 AM On 1 , 10:58, Marc Gravell <marc.grav...@gmail.com> wrote:
> > Valid parameters are List<SomeInfo>, List<string>, List<Guid>, e.t.c.
> > Invalid parameters are SomeInfo[], string[], Array, ArrayList,
>
> For info, if you want it to additionally work with arrays [i.e. T[],
> not Array], (typed) collections, etc - then another option is to use
> the less specific IList<T> interface:
>
> void Foo<T>(IList<T> list)
> {
>
> }
>
> This obviously won't give you as many methods etc as List<T>, but it
> should allow most options. In .NET 3.5, extension methods (in
> System.Linq) mean that IList<T> exposes almost as much functionality
> as List<T> does (albeit via subtly different implementation).
>
> Marc

Thanks for suggestion