Palkupz

Hi! newbie here just asking which of the following code is more efficient

1.

Code Snippet

DataRow[] dataRowArray = dtDataTable.Select("TableColumn = 1");

foreach(DataRow drRow in dataRowArray)

{
...some processing here

}

OR

2.

Code Snippet

foreach(DataRow drRow in dtDataTable.Select("TableColumn = 1"))

{

...some processing here

}

my problem here is do I need to put the result in a datarow array to make it more

efficient or not an issue at all




Re: Visual C# General Datarow Array with foreach statement question

decyclone

Check out this example :

class MyEnumerable : IEnumerable<String>
{
#region IEnumerable<string> Members

public IEnumerator<string> GetEnumerator()
{
MessageBox.Show("Starting Enumerator.");
for (int Index = 0; Index < 10; Index++)
{
yield return String.Format("Line {0}", Index);
}
}

#endregion

#region IEnumerable Members

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

#endregion
}

Execution Code :

private void FormWithCollectionTest_Load(object sender, EventArgs e)
{
foreach (String Line in new MyEnumerable())
{
MessageBox.Show(Line);
}
}

If you run this code, the message box will show up only once, thus I assume the GetEnumerator() function is called only once. The object returned by it is stored in memory and then the foreach loop uses that anonymous object to enumerate.

So, if you explicitly declare a variable to store the object returned by the method or just put the method call directly into the foreach loop, I don't see any major efficiency differences.

Hope you get it.





Re: Visual C# General Datarow Array with foreach statement question

Merin Gazell

Generally the first approach is better. In the second approach, the DataRow() array is created implicitly even if you dont.



Re: Visual C# General Datarow Array with foreach statement question

Indian Ocean

Hi,

IMHO, both the ways are same.

The reason is, second code internally does the same thing like what you are doing in first code snippet.

HTH,






Re: Visual C# General Datarow Array with foreach statement question

Merin Gazell

It helps the compiler to know what it is dealing with beforehand. Thats why I said the first approach was preferred even though what was happening was the same.



Re: Visual C# General Datarow Array with foreach statement question

Peter Rex

I think both approaches has the same perfomance time (does the same thing behind) , but only thing is the code readability or understandability is good in the first approach.



Re: Visual C# General Datarow Array with foreach statement question

Palkupz

Peter Rex wrote:
I think both approaches has the same perfomance time (does the same thing behind) , but only thing is the code readability or understandability is good in the first approach.

even if you don't use the datarow array






Re: Visual C# General Datarow Array with foreach statement question

IsshouFuuraibou

There is a datarow array created in both cases. Plus you are using the datarow array, in your foreach statement.

If you don't use the datarow array in the first example, it will have a lifespan about the same as the datarow array generated in the second.

Foreach evaluates the "Enumeration" first before enumerating through it.
dtDataTable.Select("TableColumn = 1") returns a DataRow[], so even if you were to set it in the foreach, it will still generate a DataRow[] for the enumeration to occur over.

If you have any doubts, single step through the code and watch what code gets operated on first in both cases.





Re: Visual C# General Datarow Array with foreach statement question

Ting Wang - MSFT

They are just the same. If you compile the code with "Release" mode (e.g. with optimization), there will be no difference between the results.