'How do I default a c# Func optional parameter [duplicate]
I would like a Func<float,float> parameter to a method but default it to a static function:
public Animation(float timeForAnimation,Vector target, Func<float, float> ease = Ease.nothing)
where Ease.nothing is a static function:
public static float nothing(float f)
{
return MathUtil.clamp(0, f, 1);
}
I am getting an error: 'Default parameter value for 'ease' must be a compile-time constant' .. not sure what that means or how i can get it to work
Solution 1:[1]
You don't. Default values of parameters must be constants. In this case, the only such a constant is null
.
What you can do, is resolve that within the body of Animation
.
public void Animation(float timeForAnimation,Vector target, Func<float, float> ease = null)
{
// in case of 'null' call the default Ease.Nothing() method
var easedTime = (ease ?? Ease.Nothing)(timeForAnimation);
}
Side note: Let me suggest you stick to the UpperCamelCase naming convention for method names.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |