C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




which is more effitient for loop or for each loop


Posted Date: 02 Sep 2008      Total Responses: 4

Posted By: sweety       Member Level: Gold     Points: 1



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




Responses

Author: Ashok Kandasamy    02 Sep 2008Member Level: DiamondRating:     Points: 2

Hi,
you can refer this URL

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



Author: ramya    02 Sep 2008Member Level: GoldRating:     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:     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.



Post Reply
You must Sign In to post a response.
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   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use