Featured post

c# - Usage of Server Side Controls in MVC Frame work -

i using asp.net 4.0 , mvc 2.0 web application. project requiremrnt have use server side control in application not possibl in noraml case. ideally want use adrotator control , datalist control. i saw few samples , references in codepleax mvc controllib howwver found less useful. can tell how utilize theese controls in asp.net application along mvc. note: please provide functionalities related adrotator , datalist controls not equivalent functionalities thanks in advace. mvc pages not use normal .net solution makes use of normal .net components impossible. a normal .net page use event driven solution call different methods service side mvc use actions , view completly different way handle things. also, mvc not use viewstate normal .net controlls require. found article discussing mixing of normal .net , mvc.

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.


Comments

Popular posts from this blog

c# - Usage of Server Side Controls in MVC Frame work -

cocoa - Nesting arrays into NSDictionary object (Objective-C) -

ios - Very simple iPhone App crashes on UILabel settext -