C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » .NET »

which is more effitient for loop or for each loop


Posted Date: 02 Sep 2008      Posted By: sweety      Member Level: Silver     Points: 1   Responses: 4



which is more effitient for loop or for each loop performancewise ?




Responses

Author: Ashok Kandasamy    02 Sep 2008Member Level: DiamondRating: 2 out of 52 out of 5     Points: 2

Hi,
you can refer this URL

http://www.codeproject.com/KB/cs/foreach.aspx



Author: ramya    02 Sep 2008Member Level: GoldRating: 4 out of 54 out of 54 out of 54 out of 5     Points: 6

if u want to check condition within the collection then go for foreach loop
eg:foreach(controls i in this.controls)
{
if(i is textbox)
i.color=color.red;
}

If u r going to check the value within the loop then go for this

eg:
int x=0;
for(int i=0;i<10;i++)
{
x=x+i;
}



Author: G.Hemadribabu    02 Sep 2008Member Level: GoldRating: 2 out of 52 out of 5     Points: 2

As per my knowledge is concerned Using For loop is gives good performance. You to can try it and check the perfomance of the loop , then u can judge which is better.

Hope i had answered

thank you
hemadribabu



Author: Gaurav Agrawal    02 Sep 2008Member Level: DiamondRating:     Points: -20

There are situations in which "for" is definitely worse, as well. For
example, if you have a property that builds and returns an aggregate
structure such as an array:

public MyThing[] MyThings
{
get { return
(MyThing[])this._internalMyThings.ToArray(typeof(MyThing)); }
}

(Yes, I know that this is bad practice, and that FxCop specifically
warns against it, but say you had a property like this....)

Then this

for (int x = 0; x < myObj.MyThings.Length; x++)
{
MyThing thing = myObj.MyThings[i];
}

gives absolutlely horrible performance, while this:

foreach (MyThing thing in myObj.MyThings)
{
}

gives good performance.

Again, it all depends on what you're doing, and in the end it's
usually bad designs that kill performance, not using one style of loop
versus another.

Thanks & Regards,

Gaurav Agrawal
Sr.Software Engineer
gaur1982@yahoo.com
09829373514



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : preview of pdf file is not coming in PrintPreviewControl
Previous : CONVERTING A PROJECT FROM VS2003 TO VS2005
Return to Discussion Forum
Post New Message
Category: .NET

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use