Click here to Skip to main content
16,023,224 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
In my c# application I need to pass a list of EmpIds as a parameter to a method()

Like
DisplayName (string[] ids);

or
DisplayName (List<string> ids);


Which one would you prefer: array or list.
Please consider performance as well while answering.

[edit]Tags, codeblock moved to around code, English and punctuation tidied. - OriginalGriff[/edit]
Posted
Updated 10-Jul-10 23:04pm
v2

A lot has been said on this topic - see here[^].
 
Share this answer
 
Comments
Nish Nishant 11-Jul-10 12:08pm    
Reason for my vote of 5
Good link!
Performance wise, there won't be a difference in the method call - all that is passed is a single reference in either case.

Which is best?
Depends on what you are doing. If it is a fixed array of strings, where the number of strings is known at compile time, and they do not change number, content or order at run time, then an array may be best.

If the number is not known, then List. If the order or number of items changes, List.

There is no "best" solution - it will depend on your code, and what you are trying to achieve. Bear in mind that a List can be converted to an array at any time...
 
Share this answer
 
Comments
Nish Nishant 11-Jul-10 12:09pm    
Reason for my vote of 5
Worth a 5!
If the function needs purely readonly access to the collection, you could use IEnumerable<string> or ReadOnlyCollection<string>. That way it will not be able to modify the collection. With the array, the risk is merely that of modified entries, the length cannot be changed. With the list, in addition to the risk of modified entries, the function can also remove/add entries and thus change the length of the collection. So there's that to consider too.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900