Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IEnumerable<string> and string[]

Tags:

c#

.net-4.0

Is there any advantage to using this

 private static IEnumerable<string> GetColumnNames(this IDataReader reader)
        {
             for (int i = 0; i < reader.FieldCount; i++)
                yield return reader.GetName(i);

        }

instead of this

  private static string[] GetColumnNames(this IDataReader reader)
    {
        var columnNames = new string[reader.FieldCount];
        for (int i = 0; i < reader.FieldCount; i++)
             columnNames[i] = reader.GetName(i);

        return columnNames;
    }

Here is how I use this method

    int  orderId = _noOrdinal;
    IEnumerable<string> columnNames = reader.GetColumnNames();
    if (columnNames.Contains("OrderId"))
           orderId = reader.GetOrdinal("OrderId");
     while (reader.Read())
            yield return new BEContractB2CService
                     {
                         //..................
                         Order = new BEOrder
                          {  Id = orderId == _noOrdinal ? 
                                          Guid.Empty : reader.GetGuid(orderId)
                          },
 //............................
like image 480
Alexandre Avatar asked Dec 12 '22 12:12

Alexandre


2 Answers

The two approaches are quite different so it depends on what you are subsequently going to do with the result I would say.

For example:

The first case requires the data reader to remain open until the result is read, the second doesn't. So how long are you going to hold this result for and do you want to leave the data reader open that long.

The first case is less performant if you are definitely going to read the data, but probably more performant if you often don't, particularly if there is a lot of data.

The result from your first case should only be read/iterated/searched once. Then second case can be stored and searched multiple times.

If you have a large amount of data then the first case could be used in such a way that you don't need to bring all that data in to memory in one go. But again that really depends on what you do with the IEnumerable in the calling method.

Edit: Given your use-case the methods are probably pretty much equivalent for any given measure of 'good-ness'. Tables don't tend to have many columns, and your use of .Contains ensures the data will be read every time. Personally I would stick with the array method here if only because it's a more straightforward approach.

What's the next line of the code... is it looking for a different column name? If so the second case is the only way to go.

like image 102
James Gaunt Avatar answered Jan 01 '23 18:01

James Gaunt


On reason off the top of my head: The array version means you have to spend time building the array first. Most of the code's clients may not necessarily need a specific array. Mostly, i've found, that most code is just going to iterate over it in which case, why waste time building an array (or list as an alternative) you never actually need.

like image 25
Colin Mackay Avatar answered Jan 01 '23 18:01

Colin Mackay