I know that I can declare delegates that have default constant parameters such as:
delegate void MyDelegate(int x, int y = 5);
and then call it anywhere with any method that matches the signature.
Ok, I’ve got LOTS of methods declared like this way:
public Something FirstMethod(float val = 10, int skip = 0){ ... return sth; }
public Something SecondMethod(float val = 20, int skip = 0){ ... return sth; }
public Something ThirdMethod(float val = 5, int skip = 0){ ... return sth; }
… this list goes up all the way down, anyway, they all have that signature structure. The point here is that, they all have floating point argument, that defaults to something different.
Then, I want to create a delegate that will point to one of these methods:
delegate Something ProblematicDelegateType(<<WHAT WILL GO HERE>>);
ProblematicDelegateType myFunc;
if(someValue == someParameter){
myFunc = FirstMethod;
}else if(...){
myFunc = SecondMethod;
}else...
...
}
myFunc();
myFunc(skip:100);
I want to be able to call myFunc both parameterless or with the skip parameter. In this part of code, the first parameter, val, is NOT used. (they are used elsewhere).
What will go to the delegate’s argument list? I want to preserve that method’s default val argument, whatever it is.
If I understand you correctly, your
myFuncwill leave out thevalparameter entirely, always using the default for whichever method you’re ultimately calling, correct?If so, this should accomplish what you’re looking for: