I'm working on a library and I'm having some trouble finding the right way to come up with the right APIs structure that keeps the code efficient without the need to have a ton of different overloads for each available API.
Say I have this method:
public static Brush CreateDummyBrush(Color tint, float mix, float blur)
Now, I want to let the user also receive an EffectAnimation
instance for both those tint and blur animations, to let him animate them in the future. Suppose that an EffectAnimation
is just a delegate
that starts an animation on the returned Brush
. The full code is here, just in case.
public static Brush CreateDummyBrush(
Color tint, float mix, out EffectAnimation mixAnimation,
float blur, out EffectAnimation blurAnimation)
Now, this is perfectly fine if the user wants to receive a delegate
for both those animations. But, say I only need to animate the blur effect, then I'd call this API like this:
Brush brush = CreateDummyBrush(Colors.Gray, 0.6f, out _, 8, out var blurAnimation);
This works, but the library ends up allocating a useless delegate
that serves no purpose, as it's just discarded.
The quick solution would be to create 4 different overloads, to cover the various combinations of out
parameters. But is definitely not the right approach: too much clutter, and the number of APIs becomes too large if I have, say, 3 out
parameters.
So my question is: can the caller check whether an input out
parameter is actually a valid reference, or if the discard identifier has been used? I know C# has a ton of hidden tricks and APIs, is there something that would be able to achieve this?
Thanks a lot!