Home > Net >  Easier way to make function usable with different types
Easier way to make function usable with different types

Time:08-13

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.

CodePudding user response:

I was able to bring it down from 10 to 4 variations using some recommendations from the comments.

public static int Add(params int[] numbers)
{
    return Add(numbers);
    //Creates array and calls the next function
}

public static int Add(IEnumerable<int> numbers)
{
    int result = 0;

    foreach (int n in numbers) result  = n;

    return result;
}


public static float Add(params float[] numbers)
{
    return Add(numbers);
    //Creates array and calls the next function
} 

public static float Add(IEnumerable<float> numbers)
{
    float result = 0;

    foreach (float n in numbers) result  = n;

    return result;
}

If you have any more tips please feel free to share them.

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.

UPDATE: I tried changing IEnumerable<> to List<> and it gives me no errors even if the parameter is an array and not a list, I have not yet tested it's functionality though.

CodePudding user response:

C# 11 fixes precisely this problem with INumber<T> interface.

https://youtu.be/1K44Nu9_7U8?t=19

Edit: Adding an example with generics since Unity engine might not support C# 11 yet.

namespace GenericCalculations
{
    public class GenericMath
    {
        public static object Add<T>(List<T> listOfT)
        {
            if (typeof(T) == typeof(int))
            {
                return listOfT.Sum(t => t as int?);
            }
            if (typeof(T) == typeof(double))
            {
                return listOfT.Sum(t => t as double?);
            }
            // etc 
            throw new ArgumentException("Invalid entry");
        }
    }

    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
            Console.WriteLine(GenericMath.Add(new List<double>() { 1.1, 1.2, 1.3 }));
            Console.WriteLine(GenericMath.Add(new List<int>() { 1, 1, 1 }));

        }
    }
}
  •  Tags:  
  • c#
  • Related