Featured post
c# - When to use each type of loop? -
i'm learning basics of programming here (c#) think question generic in nature.
what simple practical situations lend closer particular type of loop?
the while
, for
loops seem pretty similar , there several questions addressing differences between two. how foreach
? basic understanding, seems ought able foreach
loop within for
loop.
1. foreach , for
a foreach
loop works ienumerator, when for
loop works index (in object myobject = mylistofobjects[i]
, index).
there big difference between two:
an index can access directly object based on position within list.
an enumerator can access first element of list, , move next element (as described in previous link msdn). cannot access element directly, knowing index of element within list.
so enumerator may seem less powerful, but:
- you don't know position of elements in group, because groups not ordered/indexed.
- you don't know number of elements in list (think linked list).
- even when it's ordered, indexed access of list may based internally on enumerator, means each time you're accessing element position may enumerating elements of list until element want.
- indexes not numeric. think dictionary.
so big strength of foreach
loop , underlying use of ienumerator
applies type implements ienumerable
(implementing ienumerable means provide method returns enumerator). lists, arrays, dictionaries, , other group types implement ienumerable. , can sure enumerator have gets: won't find fastest way go through list.
so, for
loop can generally considered specialized foreach
loop:
public void gothrough(list<object> mylist) { (int i=0; i<mylist.count; i++) { messagebox.show(mylist[i].tostring()); } }
is equivalent to:
public void gothrough(list<object> mylist) { foreach (object item in mylist) { messagebox.show(item.tostring()); } }
i said generally because there obvious case when for
loop necessary: when need index (i.e. position in list) of object, reason (like displaying it). though realize this happens in specific cases when .net programming, , foreach
should default candidate loops on group of elements.
now keep comparing foreach
loop, indeed eye-candy specific while loop:
public void gothrough(ienumerable myenumerable) { foreach (object obj in myenumerable) { messagebox.show(obj.tostring()); } }
is equivalent to:
public void gothrough(ienumerable myenumerable) { ienumerator myenumerator = myenumerable.getenumerator(); while (myenumerator.movenext()) { messagebox.show(myenumerator.current.tostring()); } }
the first writing lot simpler though.
2. while , do..while
the while (condition) {action}
loop , do {action} while (condition)
loop differ each other fact first 1 tests condition before applying action, when second 1 applies action, tests condition. do {..} while (..)
loop used quite marginally compared others, since runs action @ least once if condition not met (which can lead trouble, since action dependent on condition).
the while
loop more general for
, foreach
ones, apply lists of objects. while
loop has condition go on, can based on anything. example:
string name = string.empty; while (name == string.empty) { console.writeline("enter name"); name = console.readline(); }
asks user input name press enter, until inputs something. nothing lists, can see.
3. conclusion
when going through list, should use foreach
unless need numeric index, in case should use for
. when doesn't have list, , it's procedural construction, should use while(..) {..}
.
now conclude less restrictive: first goal .net should make code readable/maintainable , make run fast, in order of priority. achieves you. though, think foreach
loop has advantage potentially, it's readable and fastest.
edit: there other case for
loop useful: when need indexing go through list in special way or if need modify list when in loop. example, in case because want remove every null element mylist:
for (int i=mylist.count-1; i>=0; i--) { if (mylist[i] == null) mylist.removeat(i); }
you need for
loop here because mylist cannot modified within foreach
loop, , need go through backwards because if remove element @ position i, position of elements index >i change.
but use these special constructions have been reduced since linq. last example can written in linq example:
mylist.removeall(obj => obj == null);
linq second step though, learn loops first.
- Get link
- X
- Other Apps
Comments
Post a Comment