c# - For-each loop variable -


what ob means in following code - same item?

foreach (var item in allitems) {     if (excludeitems.exists(ob => ob.equals(item)))     {         console.writeline("item {0} excluded",item);     } } 

ob parameter lambda expression. if you're familiar anonymous methods, it's like:

foreach (var item in allitems) {     if (excludeitems.exists(delegate (string ob) { return ob.equals(item); })     {         console.writeline("item {0} excluded",item);     } } 

that's assuming type of ob should string - may not be. depend of excludeitems, due generic type inference.

lambda expressions can more explicit, could written as:

if (excludeitems.exists((string ob) => { return ob.equals(item); }) 

or

if (excludeitems.exists((string ob) => ob.equals(item)) 

basically there several little shortcuts in lambda expressions common case of single parameter type can inferred, , return value single expression.

now in particular case, delegate created lambda expression called once each element in excludeitems (in each iteration of foreach loop) , ob have value of element, until finds value equal item (or runs out of elements).


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -