Passing a conditional Type to an overloaded method...

I know this has come up and been answered before, but I’m not finding it. (There is probably a term for it that I am not recalling.). As very simple example:

            private bool use_ease = true;
            private Ease ease;
            private AnimationCurve curve;
           
            transform.DOMoveZ(1f,1f).SetEase((use_ease) ? ease : curve);

Any thoughts? Do I need to clarify?
TIA, ZG

The ternary operator can’t return results of two different types; the entire expression is either an Ease or an AnimationCurve. So you’d have to do something like an if/else around the whole call.

1 Like

Yea, doesn’t need to be a ternary specifically, might have been a cast or something else. Someone had posted an example a while ago, but I can’t seem to find it.

The runtime figures out which override of a method it’s supposed to use at runtime. Overloads, on the other hand, has to be figured out at compile time.

What you’re trying to do there is to dynamically select which of two overloads should be used. That doesn’t work in C#.

There’s weirdnesses with how the ternary operator works with assignment in C#. This is not one of those, though.