c# - When to use each of T[], List<T>, IEnumerable<T>? -


I usually get to do something like this:

  string [] things = ArrayReturningMethod (); Int index = cheese. Toolist & lt; String & gt; INDIDEX ((s) => gt; s.Equals ("FOO")); // Do something with index return thing Disting (); // which is an IEnumerable & lt; String & gt; gives.  

And I find this kind of interface / interface a bit confusing and it tickles my potential performance problem antenna (which is not right until I prove it right).

Is this idiomatic and appropriate C # or is there a better option to avoid back and forth molding to reach the proper ways to work with the data?

Edit: The question is really double:

  • Whenever either IEnumerable interface or an array or a list (or any other IEnumerable implementation type) Usage is right (when accepting parameters)?

  • Do you want to move freely between IainumSharshals (Implementation Unknown) and lists and IEmerebureables and arrays and arrays and lists or what is non idiomatic (there are better ways to do this ) / Non Performer (usually not relevant), but in some cases) / Just plain ugly (unmaintable, unreadable)?

Regarding display ...

  • Changing from list to t [] involves copying all the data from the original list into a new allocated array.
  • Change from T [] to the list includes copying all the data from the original list into a new allotted list.
  • Either convert to list or T [] contains castings in IEnumerable, which has some CPU cycles.
  • An exception to changing the list from IEnumerable, which includes some CPU cycles.
  • Changing from IEnumerable to T [] also includes upachings.
  • You can not put an IEnumerable in T [] or list unless you can use ToArray or ToList functions to start with a T [] or list respectively. With this, a copy will also be made.
  • To gain access to all values ​​in the beginning of a T [], in a straight loop, optimized to use direct indicator math - which makes it the fastest of all of them.
  • Receiving all the values ​​from the beginning to the end of the list involves an investigation on each recurring that to ensure that you reach a value outside the 'array range, and then the array value Actual access to
  • To create a calculator object in all the values ​​in an IEnumerable, call the next () function that increases the indicator indicator, and then calls the current property which gives you the actual value and that Sticks in the variable that you specified in your antecedent detail. Generally, it is not as bad as it sounds.
  • Access to an arbitrary value in an IEnumerable starts at the beginning and calls to the next () as often as you should go to that value. Generally, this is as bad as it sounds.

In relation to idioms ...

In general, IEnumerable is useful for public properties, function parameters and often for return value - and only if You know that you are using values ​​in sequentially. For example, if you have printvelues, then this printw (list & lt; T> value) will be able to deal with list values ​​only, so the user must first change, for example if they [t] The function was similar with the use of printlights (T [] value). But if it was PrintValues ​​(IEnumerable & lt; T> value), then it would be able to deal with the list, T [] s, stacks, hashtables, dictations, string, set, etc. - any collection that is available in IEmable Implementation, which practically every collection

Regarding internal use ...

  • Use a list only if you are not sure how many items it will need.
  • If you know how many items will be needed in it, but use a T [], but an arbitrary value is required to be used.
  • If you have it then stay with IEnumerable and you need to use it sequentially. Many tasks will come back to the Inamerables if you need to access an IEnumerable values ​​in arbitrary order, use ToArray (). Also, keep in mind that castings are different from using ToArray () or ToList () - later the copy of the values ​​is included, which in fact will hit a display and memory. You have many elements in the past only to say that "dog is an animal, like any animal it can eat" (indifference) or "this animal is a dog, so it can bark" ( Upcast). Similarly, all lists and T [] are IEnumerables, but only a few IEnumerables lists or T [] s.


Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -