Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
foreach (DataRow dr in dt.Rows)
{
}

Can u explain what checks this foreach loop?
I am a little confuse between for and foreach, explain wt is difference betweeen for and foreach.
Posted
Updated 16-Jan-11 19:04pm
v2

 
Share this answer
 
Comments
TweakBird 17-Jan-11 3:02am    
Good Link.
Well foreach fetched one by one object among the collection and For loop fetched based on index of object.

Like if you want to apply for loop for the same scenario then

For(int i=0;i<dt.rows.count;i++)
{
DataRow row = dt.rows[i];
}


Where i Plays an important role that on object at which index should be fetched. and while in foreach it grabs one by one object from the collection.

Article provided by thatraja is good but it lacks understanding may THIS[^] will help you in better way to understand it.
 
Share this answer
 
v2
Hi,
A for loop is run on a condition, and will run until the
condition is false. Usually this is a counter, and the
condition is a threshold.

Ex for(int i=0;i<dt.Rows.count;i++)
{
}
Here counter is run until it is not setisfied i<dt.Rows.count.
So here you can take any number for condition.

A foreach runs through an array
executing the code in the the loop once for each element of
the array, with the array element.

Ex .
foreach (DataRow dr in dt.Rows)
{
}

Here dr checks each element of the dt.rows
 
Share this answer
 
Comments
Аslam Iqbal 17-Jan-11 1:45am    
Actually a foreach runs through a collection.
It checks the each element of the rows of Datatable.

FOREACH Vs. FOR (C#)[^]

Difference between For and Foreach loop[^]
 
Share this answer
 
v2
Comments
kmon123 24-Nov-14 13:08pm    
For each ( datarow dr in dt.rows )
{
Picture [I] =new Category()
{
CategoryId = convert.toint32(dr["CategoryId"]), Picture = (byte[]) dr ["picture"]
};
I = I +1;

Pls help me I get error here, trying to convert images to byte and store in sql
Member 12941337 16-Jan-17 2:31am    
h

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900