Simulate variadic templates in C#

C# generics are not the same as C++ templates. C++ templates are expanded compiletime and can be used recursively with variadic template arguments. The C++ template expansion is actually Turing Complete, so there is no theoretically limit to what can be done in templates.

C# generics are compiled directly, with an empty "placeholder" for the type that will be used at runtime.

To accept a lambda taking any number of arguments you would either have to generate a lot of overloads (through a code generator) or accept a LambdaExpression.


There is no varadic support for generic type arguments (on either methods or types). You will have to add lots of overloads.

varadic support is only available for arrays, via params, i.e.

void Foo(string key, params int[] values) {...}

Improtantly - how would you even refer to those various T* to write a generic method? Perhaps your best option is to take a Type[] or similar (depending on the context).


I know this is an old question, but if all you want to do is something simple like print those types out, you can do this very easily without Tuple or anything extra using 'dynamic':

private static void PrintTypes(params dynamic[] args)
{
    foreach (var arg in args)
    {
        Console.WriteLine(arg.GetType());
    }
}

static void Main(string[] args)
{
    PrintTypes(1,1.0,"hello");
    Console.ReadKey();
}

Will print "System.Int32" , "System.Double", "System.String"

If you want to perform some action on these things, as far as I know you have two choices. One is to trust the programmer that these types can do a compatible action, for example if you wanted to make a method to Sum any number of parameters. You could write a method like the following saying how you want to receive the result and the only prerequisite I guess would be that the + operation works between these types:

    private static void AddToFirst<T>(ref T first, params dynamic[] args)
    {
        foreach (var arg in args)
        {
            first += arg;
        }
    }

    static void Main(string[] args)
    {
        int x = 0;
        AddToFirst(ref x,1,1.5,2.0,3.5,2);
        Console.WriteLine(x);

        double y = 0;
        AddToFirst(ref y, 1, 1.5, 2.0, 3.5, 2);
        Console.WriteLine(y);

        Console.ReadKey();
    }

With this, the output for the first line would be "9" because adding to an int, and the second line would be "10" because the .5s didn't get rounded, adding as a double. The problem with this code is if you pass some incompatible type in the list, it will have an error because the types can't get added together, and you won't see that error at compile time, only at runtime.

So, depending on your use case there might be another option which is why I said there were two choices at first. Assuming you know the choices for the possible types, you could make an interface or abstract class and make all of those types implement the interface. For example, the following. Sorry this is a bit crazy. And it can probably be simplfied.

    public interface Applyable<T>
    {
        void Apply(T input);

        T GetValue();
    }

    public abstract class Convertable<T>
    {
        public dynamic value { get; set; }

        public Convertable(dynamic value)
        {
            this.value = value;
        }

        public abstract T GetConvertedValue();
    }        

    public class IntableInt : Convertable<int>, Applyable<int>
    {
        public IntableInt(int value) : base(value) {}

        public override int GetConvertedValue()
        {
            return value;
        }

        public void Apply(int input)
        {
            value += input;
        }

        public int GetValue()
        {
            return value;
        }
    }

    public class IntableDouble : Convertable<int>
    {
        public IntableDouble(double value) : base(value) {}

        public override int GetConvertedValue()
        {
            return (int) value;
        }
    }

    public class IntableString : Convertable<int>
    {
        public IntableString(string value) : base(value) {}

        public override int GetConvertedValue()
        {
            // If it can't be parsed return zero
            int result;
            return int.TryParse(value, out result) ? result : 0;
        }
    }

    private static void ApplyToFirst<TResult>(ref Applyable<TResult> first, params Convertable<TResult>[] args)
    {
        foreach (var arg in args)
        {                
            first.Apply(arg.GetConvertedValue());  
        }
    }

    static void Main(string[] args)
    {
        Applyable<int> result = new IntableInt(0);
        IntableInt myInt = new IntableInt(1);
        IntableDouble myDouble1 = new IntableDouble(1.5);
        IntableDouble myDouble2 = new IntableDouble(2.0);
        IntableDouble myDouble3 = new IntableDouble(3.5);
        IntableString myString = new IntableString("2");

        ApplyToFirst(ref result, myInt, myDouble1, myDouble2, myDouble3, myString);

        Console.WriteLine(result.GetValue());

        Console.ReadKey();
    }

Will output "9" the same as the original Int code, except the only values you can actually pass in as parameters are things that you actually have defined and you know will work and not cause any errors. Of course, you would have to make new classes i.e. DoubleableInt , DoubleableString, etc.. in order to re-create the 2nd result of 10. But this is just an example, so you wouldn't even be trying to add things at all depending on what code you are writing and you would just start out with the implementation that served you the best.

Hopefully someone can improve on what I wrote here or use it to see how this can be done in C#.