To add to the other answers, personally I would have two separate methods (i.e. just use Math.Max
or Math.Min
) but if you need one method, I would use a bool
instead of a string
. One main reason for that is that you avoid case-sensitive issues and since there are only two options, a boolean is suited to that. Alternatively, you could use an Enum.
public static int FindMinOrMax(int x, int y, bool findMax)
{
return findMax ? Math.Max(x, y) : Math.Min(x, y);
}
EDIT From the other comments, I've understood what you're trying to achieve. Whilst I don't recommend it, you could do this
public static int FindMinMax(int x, int y, string method)
{
MethodInfo mi = typeof(Math).GetMethod(method, new[] { typeof(int), typeof(int) });
return (int)mi.Invoke(null, new object[] { x, y} );
}
Method is case-sensitive so Max
will work but max
won't as the method is called Max
. You can't just capitalise the first letter all the time (unless it's just Min
/Max
) as DivRem
has camel-casing.
+1 to Toto as he/she answered first, I just edited and noticed but will keep this post for completion