c# - How to define variable as lambda function -
c# - How to define variable as lambda function -
(lambda function may or may not i'm looking for, i'm not sure)
essentially i'm trying accomplish this:
int areaofrectangle = (int x, int y) => {return x * y;};
but gives error: "cannot convert lambda look type 'int' because not delegate type"
the more detailed problem (that has nil question, know ask) is:
i have several functions branch overridden onlayout , several more functions each of depend on. readability , set precedent later expansion, want functions branch onlayout similar. that, need compartmentalize them , reuse naming much possible:
protected override void onlayout(layouteventargs levent) switch (layoutshape) { case (square): dosquarelayout(); break; case (round): doroundlayout(); break; etc.. etc.. } void dosquarelayout() { part layershape = (int layer) => { //do calculation homecoming new region(math.ceiling(math.sqrt(itemcount))); } int gradientangle = (int itemindex) => { //do calculation homecoming ret; } //common-ish layout code uses layershape , gradientangle goes here } void doroundlayout() { part layershape = (int layer) => { //do calculation graphicspath shape = new graphicspath(); shape.addellipse(0, 0, width, height); homecoming new region(shape); } int gradientangle = (int itemindex) => { //do calculation homecoming ret; } //common-ish layout code uses layershape , gradientangle goes here }
all examples find right have declare delegate know i've seen 1 liner lambda declaration...
try func<int, int, int> areaofrectangle = (int x, int y) => { homecoming x * y;};
instead.
func
works as delegate
look here more info on lamda look usage
this reply related , has info
if you're doing readability , going reproduce same functions layershape
, gradientangle
, might want have explicit delegates functions show in fact same. thought.
c# lambda
Comments
Post a Comment