should i use var or toArray() to convert System.Collections.Generic.IEnumerable<string> to string[] RRS feed

  • Question

  • User2102072086 posted


       which one is the best way of doing the following and why? 

    string[] doubleArray = Regex.Split(strValue, @"[^0-9\.]+").Where(c => c != "." && c.Trim() != "").ToArray();  // here i have used toarray 

    var doubleArray = Regex.Split(strValue, @"[^0-9\.]+").Where(c => c != "." && c.Trim() != ""); // and here i have used var

    yours sincerley

    Wednesday, July 17, 2019 12:25 PM

All replies

  • User-821857111 posted

    In the first, you get a string array. The second returns an IEnumerable<string>. They are different types, but they share similarities in terms of how you can work with them. They both allow you to iterate over the contents, for example.  Ultimately, the best way to do this is depends on what return type you want. 

    Wednesday, July 17, 2019 12:45 PM
  • User2102072086 posted

    we just want to iterate and get the value from the array.

    Wednesday, July 17, 2019 1:11 PM
  • User-821857111 posted

    It depends on how you want to iterate. You can use foreach on both types, but you cannot use a for loop with an indexer on an IEnumerable. So the following won't work on the second example:

    for(var i = 0; i < doubleArray.Count(); i++)

    whereas it will work on the first example.

    Wednesday, July 17, 2019 1:20 PM
  • User753101303 posted


    It rather depends on what you'll do next with this variable ie do you need later to use something that is particular to a string[] ?

    The advice is often to use the more general type needed to write your code (ie IEnumerable<string> if enough, List<string>, string[] or some other data structure if really needed).

    If you are new to interfaces try https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/interfaces/

    In short it allows to write code against objects based on what they can do rather than what they actually are.

    Wednesday, July 17, 2019 1:21 PM
  • User2102072086 posted

    lets say i have to only use foreach in this case. then which one i should follow?

    and i noticed one more thing the second one when iterates it goes back to the .where() "Regex.Split(strValue, @"[^0-9\.]+").Where(c => c != "." && c.Trim() != "");"

    foreach (var item in doubleArray.AsQueryable())



    i think it needs somthinglike  tolist(), please suggest what should be used.

    Thursday, July 18, 2019 7:40 AM
  • User303363814 posted

    should i use var or toArray() to convert System.Collections.Generic.IEnumerable<string> to string[]

    Well ... var does not convert anything to anything so that is not an option.  var just says to the compiler "I can't be bothered working out/typing this boring Type information, you work it out for me".  var just makes the compiler do a little bit of the work for you.  Hooray! Less work for me.

    You seem to want to use foreach with the result of this splitting/filtering operation.  foreach requires an iterator.  The result of the Where clause is something that can be used as an iterator.

    Pretty clearly, the fastest, neatest, easiest thing to do is nothing!

    foreach (var item in Regex.Split("mumble").Where("whatever"))
    // Go for it

    Do nothing is always best.  Less typing, less chance of making an error.

    Thursday, July 18, 2019 10:52 PM