Yes. it creates a new object every time. but the scope is limited within the for each loop only. so you have no problem. Then foreach is better than ordinary for loop, becoz, for loop, you need to specify the upper and the lower bounds, which is every time
you need to calculate whenever you need to loop.
Marking a Reply as 'Answered', not only GAIN us some POINTS, but it also HELP others to find RIGHT solution.
but foreach is lot easier to use and its code is lot more readable.
Now as in most part of your code you need to put in small loops, so its best to use
foreach then to make your code more readable and in these scenarios the performance benefit given by
for loop is not much considerable and prefer to use for
loops in performance critical parts.. Please check the following article.
sudheepdivak...
Member
2 Points
16 Posts
Creating string object inside for-each loop
Oct 23, 2009 02:09 AM|LINK
Hi,
I have a doubt,
string[] sArray = new string["a","b","c",.....upto 100 string];
foreach (string temp in sArray)
{
Response.Write(temp);
}
will this for-each loop create new string object each time it iterates ?
Is there any advantage if I use for loop other thay for-each to iterate through string array/array list etc ??
MetalAsp.Net
All-Star
112718 Points
18367 Posts
Moderator
Re: Creating string object inside for-each loop
Oct 23, 2009 02:20 AM|LINK
There may be a bit of a performance benefit with for loops, however I find foreach more readable. I would go with foreach.
ameenkpn
Contributor
4805 Points
814 Posts
Re: Creating string object inside for-each loop
Oct 23, 2009 02:20 AM|LINK
Yes. it creates a new object every time. but the scope is limited within the for each loop only. so you have no problem. Then foreach is better than ordinary for loop, becoz, for loop, you need to specify the upper and the lower bounds, which is every time you need to calculate whenever you need to loop.
malcolms
All-Star
18687 Points
3124 Posts
MVP
Re: Creating string object inside for-each loop
Oct 23, 2009 02:28 AM|LINK
I agree with MetalAsp.Net. The foreach is much easier to read that the for loop. Code readability counts.
deepthoughts
Contributor
7288 Points
1051 Posts
Re: Creating string object inside for-each loop
Oct 23, 2009 04:55 AM|LINK
The for loop is better in performance compared to foreach loop. Please check the following article.
http://www.codeproject.com/KB/cs/foreach.aspx
but foreach is lot easier to use and its code is lot more readable.
Now as in most part of your code you need to put in small loops, so its best to use foreach then to make your code more readable and in these scenarios the performance benefit given by for loop is not much considerable and prefer to use for loops in performance critical parts.. Please check the following article.
http://dotnetperls.com/foreach
Thanks.
pl_harish
Member
23 Points
9 Posts
Re: Creating string object inside for-each loop
Oct 24, 2009 10:47 AM|LINK
Given your code,
foreach(string temp.... creates temp everytime.. a new string object.. but within the scope of the loop only.
whereas,
for(int k=0; k<sArray.length; k++)
{
Response.Write(sArray[k]);
} ..
doesn't create a new string object..
HarishPL.info