| Author: Ashok Kandasamy 02 Sep 2008 | Member Level: Diamond | Rating:  Points: 2 |
Hi, you can refer this URL
http://www.codeproject.com/KB/cs/foreach.aspx
|
| Author: ramya 02 Sep 2008 | Member Level: Gold | Rating:    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 2008 | Member Level: Gold | Rating:  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 2008 | Member Level: Diamond | Rating: 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
|