thanks objectivity, c# language of choice. angry downvoters, think i've asked legitimate question here? otherwise leave constructive comment, please.
to question...
- c++ allows passing of (generic) function pointers simply, follows: how pass generic function pointer parameter
- java uses interfaces - elegant oo perspective, use nothing more basic language supplies.
however, have never seen real advantage making delegate
explicit concept / keyword, opposed managing concept of callbacks way example c++ or java -- treating function pointers circumstance under existing type system. (p.s. yes, c# generics not same c++ generics, while java has runtime rather compile-time generics, drift).
(all hubris , dogma aside) why did designers of c# see fit give new name common, existing programming concept have been called generic function pointer / callback? delegates not have been more represented in c#, without such concept?
disclaimer i've looked @ quite number of answers on stackoverflow , not 1 of them has satisfactorily answered why designers saw fit include keyword fundamental callback handling.
c++ had "official" (non-boost) "full" delegates c++11 (std::function
)... before that, getting pointer member function little hackish... wouldn't consider c++ comparison :-) , c++ can overload round parenthesis, more easy "hide" hacks need done , give programmer "simple" way of using std::function
.
now... java... "correct" (but let's wasn't omission, calculated decision, didn't have correct) missing delegate concept first had introduce anonymous classes in java 1.1 , in java 8 introduced functional interfaces (as non-java programmer, consider last thing little hackish... meta-describing interface has single method , enforcing @ compile time... don't much...). because otherwise boilerplate code needed quite much...
let's start simple... icomparer<>
interface... interface single method, similar delegate...
a simple implementation:
public class mycomparer : icomparer<int> { public int compare(int x, int y) { return x.compareto(y); } }
there boilerplate row here, first 1 (public class mycomparer : icomparer<int>
). introducing c# delegate
gained 1 row each use of delegate... wait! let's "delegate" needs reference class "contains" it...
public class mycomparer : icomparer<int> { public myclass target; public int compare(int x, int y) { return target.ascending ? x.compareto(y) : y.compareto(x); } }
and class needs nested class of myclass
. note don't have against nested classes...
we have added new line (public myclass target
)... code wrong write... shouldn't have public non-readonly fields. use auto-property public myclass target { get; set; }
synctactic sugar introduced in c# 3.0... without them boilerplate code grow... , prefer have private readonly myclass target
.. have add 4 lines constructor... how many lines want me write delegate? :-)
and still c#/.net delegates give more flexibility: function use can have name, can have multiple of them in same class... mycomparer
have been implemented 2 methods:
public int compareascending(int x, int y) {}
and
public int comparedescending(int x, int y) {}
without adding code, or splitting in multiple (nested) classes.
Comments
Post a Comment