This is what I have as an "Add" function in my Maths library:
public static int Add(int a, int b) => a + b;
public static float Add(float a, float b) => a + b;
public static int Add(int a, int b, int c) => a + b + c;
public static float Add(float a, float b, float c) => a + b + c;
public static int Add(int a, int b, int c, int d) => a + b + c + d;
public static float Add(float a, float b, float c, float d) => a + b + c + d;
public static int Add(List<int> numbers)
{
int result = 0;
foreach (int n in numbers) result += n;
return result;
}
public static float Add(List<float> numbers)
{
float result = 0;
foreach (float n in numbers) result += n;
return result;
}
public static int Add(int[] numbers)
{
int result = 0;
foreach (int n in numbers) result += n;
return result;
}
public static float Add(float[] numbers)
{
float result = 0;
foreach (float n in numbers) result += n;
return result;
}
Is there any way to achieve the same result (having a function that works both with int and float and with different parameters) with fewer variations of this function (just to make the code easier to understand)?
EDIT: I'm working with Unity 2022.1.7f1, so for everyone suggesting to use generic math, I don't believe Unity supports C# 11 yet. Please correct me if I'm wrong.