473,385 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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

Nov 1 '07 #1
4 3170
On Nov 1, 8:29 am, marss <marss...@gmail.comwrote:
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<Tlist)
{
}

Jon

Nov 1 '07 #2
On 1 , 10:50, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Nov 1, 8:29 am, marss <marss...@gmail.comwrote:
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<Tlist)
{

}

Jon
Thanks

Nov 1 '07 #3
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<Tinterface:

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

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<Texposes almost as much functionality
as List<Tdoes (albeit via subtly different implementation).

Marc

Nov 1 '07 #4
On 1 , 10:58, Marc Gravell <marc.grav...@gmail.comwrote:
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<Tinterface:

void Foo<T>(IList<Tlist)
{

}

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<Texposes almost as much functionality
as List<Tdoes (albeit via subtly different implementation).

Marc
Thanks for suggestion

Nov 1 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: tropostropos | last post by:
On Solaris, using the Sun compiler, I get annoying warnings from the following code. The problem is that I am passing a C++ member function pointer to the C library function qsort. Is there a...
2
by: Harshankumar | last post by:
Hello, I have the following questions 1) can we pass the parameters to interrupt? 2)can we return the parameters from interrupt Regards harshan
1
by: Dotnet Gruven | last post by:
I've posted this in the adonet group, however it was suggested I might have better luck here.... ============================================================= I'm trying to use a typed dataset and...
0
by: eric | last post by:
Can I declare a pure virtual member function which accepts as input a boost shared pointer to an object of a base class, such that - concrete implementations of the function can redirect the...
3
by: neilcancer | last post by:
My English is poor... There are some sort algorithms which sort a sequencial list. The sequencial list was defined in "list-seq.h". sort_and_time() accepts one of these sort functions. It uses...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
2
by: Fabrizio Romano | last post by:
Hello, I have a problem with a generic method. I have written a sieve to generate prime numbers. This method takes an input parameter which is the upperbound of the last prime I need to get. So...
5
by: Jon Slaughter | last post by:
I have some code like if (val.GetType().Name == typeof(List<>).Name) { } which lets me determine if val is a generic list(I've already taken care of the non-generic part but I'd rather be...
9
by: Daniel | last post by:
I'm getting an error on this code that says index out of range. It's an unhandled exception error. What is the problem? List<unsigned intls(1000); ls = u ; Daniel
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.