Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# string[] vs IEnumerable<string>

Tags:

c#

What should I prefer if I know the number of elements before runtime?

Resharper offers me IEnumerable<string> instead of string[]?

like image 417
Stefan aus Linz oida Avatar asked Mar 15 '11 15:03

Stefan aus Linz oida


1 Answers

ReSharper suggests IEnumerable<string> if you are only using methods defined for IEnumerable. It does so with the idea that, since you clearly do not need the value to be typed as array, you might want to hide the exact type from the consumers of (i.e., the code that uses) the value because you might want to change the type in the future.

In most cases, going with the suggestion is the right thing to do. The difference will not be something that you can observe while your program is running; rather, it's in how easily you will find it to make changes to your program in the future.

From the above you can also infer that the whole suggestion/question is meaningless unless the value we are talking about is passed across method boundaries (I don't remember if R# also offers it for a local variable).

like image 178
Jon Avatar answered Sep 30 '22 18:09

Jon