c# - Understanding Delegates and Task -
i come conventional programming background.
i not understanding few of new concepts though have tried lot , have interest learn, please bare me.
public static task<tresult> foreachparallel<titem, tsubresult, tresult, tparam>(this ienumerable items, func<titem, tparam, tsubresult> map, func<tsubresult[], tresult> reduce, tparam param) { if (items == null) { throw new argumentnullexception("items"); } if (map == null) { throw new argumentnullexception("map"); } if (reduce == null) { throw new argumentnullexception("reduce"); } return task<tresult>.factory.startnew(() => { list<task<tsubresult>> tasks = new list<task<tsubresult>>(); foreach (titem item in items) { task<tsubresult> t = task<tsubresult>.factory.startnew(item2 => { var mparam = (tuple<titem, tparam>)item2; return map(mparam.item1, mparam.item2); }, new tuple<titem, tparam>(item, param), taskcreationoptions.none | taskcreationoptions.attachedtoparent); tasks.add(t); } list<tsubresult> results = new list<tsubresult>(); foreach (task<tsubresult> task in tasks) { results.add(task.result); } return reduce(results.toarray()); }); } static void main(string[] args) { var = generate().foreachparallel<int, int, int, int>( (element, param) => { var result = element * param; console.writeline("map: {0}, {1}", result, task.currentid); thread.sleep(new random(datetime.now.millisecond).next(500)); return result; }, (subresult) => { var sum = 0; foreach (var item in subresult) { sum += item; } console.writeline("reduce: {0}", sum); return sum; }, 5); console.writeline(a.result); } } static ienumerable generate() { (int = 0; < 100; i++) { yield return i; } }
i have few questions regarding program:
- in
main()
, why author not populating 4 values (viz, element, param,subresult)? - in
main()
, element , param values? - in
foreachparallel()
, role ofnew tuple<titem, tparam>(item, param)
? - in
foreachparallel()
,var mparam = (tuple<titem, tparam>)item2
, item2? author calling tuple function?
before delving how piece of code works there c# language theory useful know. i'll indicate link near each answer.
a).b). elements expecting populated have values when lambda expression called. see in main lambda expressions not called when declared when used. first lambda called on line return map(mparam.item1, mparam.item2);
second lambda called on line return reduce(results.toarray());
place start lambda expressions can here.
c). when startnew method called have option call different set of methods (method overloading). here call made particular method signature:
public task startnew(action<object> action, object state, taskcreationoptions creationoptions)
which means second parameter of method (state) input parameter previous action.
d). newly created tuple
object passed lambda expression:
item2 => { var mparam = (tuple<titem, tparam>)item2; return map(mparam.item1, mparam.item2); },
basically action<object>
delegate represents method accepts parameter of type object input need tuple access items (item1 , item2). line var mparam = (tuple<titem, tparam>)item2
makes explicit conversion object tuple.
to find out why 4th parameter not passed need know method extensions. in fact first parameter not passed because method called using object of type ienumerable. method extends class ienumerable without touching original class way 'i want method extend ienumerable , use every ienumerable object' setting first parameter ienumerable. compiler make sure sends list first parameter in extension method have use.
Comments
Post a Comment