Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
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 16-Jan-11 19:58pm
Edited 16-Jan-11 20:04pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

  Permalink  
Comments
E$w@r at 17-Jan-11 3:02am
   
Good Link.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Comments
Аslam Iqbal at 17-Jan-11 1:45am
   
Actually a foreach runs through a collection.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

It checks the each element of the rows of Datatable.
 
FOREACH Vs. FOR (C#)[^]
 
Difference between For and Foreach loop[^]
  Permalink  
v2
Comments
kmon123 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Please try the following link,
 
http://onlydifferencefaqs.blogspot.in/2012/07/dotnet-programming-concepts-difference.html[^]
 
to know the difference between for and for each loops in summary
  Permalink  
Comments
lewax00 at 10-Aug-12 14:14pm
   
Please stop dragging up old questions with links to your blog, you'll be flagged as a spammer.
UmarAli1981 at 11-Aug-12 2:06am
   
Thank you for your kind suggestion.You are trying to convey me, already best solution (Solution 4) is provided by Аslam Iqbal.Then,why i have provided Solution 5 with my blog link.Sorry! i understand what happened.This is because i am new for questions reply section, so i did not notice solution section marked with green color.
 
One Query Please: Is there any wrong to provide my blog link or any other web link for new or unanswered questions area ?

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 535
1 Kornfeld Eliyahu Peter 407
2 Maciej Los 369
3 DamithSL 221
4 OriginalGriff 218
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,476
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,917


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 10 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100