How create a generic Func delegate
Asked Answered
M

3

7

I have this method

    public static T F<T>(T arg)
    {
        return arg;
    }

and I want to create a Func delegate to F. I try this

public Func<T, T> FuncF = F;

but it's not syntactically correct. How I can do that.

Menadione answered 5/11, 2014 at 13:53 Comment(3)
Seems fine to me dotnetfiddle.net/JRjFxUAlwyn
@Alwyn You've added something that is not in the original question, which is why it works.Campanulaceous
@DStanley I assumed the enclosing class was generic :/ I assumed too muchAlwyn
C
9

Only classes and methods can be generic in and of themselves. A field that uses generic parameters must be within a generic class context:

public class Test<T>
{
    public static T F(T arg)
    {
        return arg;
    }

    public Func<T, T> FuncF = F;
}

Or if the type parameter for F should not be connected to FuncF, then just use a different name for one of the parameters:

public class Test<T>
{
    public static U F<U>(U arg)
    {
        return arg;
    }

    public Func<T, T> FuncF = F;
}
Campanulaceous answered 5/11, 2014 at 13:57 Comment(0)
T
2

You can only (re)expose generics from a generic class, or in a generic method. Otherwise, you will need to provide definite types for T (e.g. for a local variable or as a field or property in a non-generic class). Examples:

// I.e. Re-expose by wrapping your Func in a static class:
public static class MyFuncs
{
    public static T F<T>(T arg)
    {
        return arg;
    }
}

public class Generics<T>
{
    // Use it as a field
    private Func<T, T> f = MyFuncs.F;

    // Or getter func
    public Func<T, T> FuncF()
    {
        return MyFuncs.F;
    }
}

// Re-expose generic via Generic Method
public class GenericMethod
{
    public Func<T, T> FuncF<T>()
    {
        return MyFuncs.F;
    }
}


// i.e. Consume the generic and do not re-expose it
public class SpecificClass
{
    public Foo FuncF(Foo f)
    {
        return MyFuncs.F<Foo>(f); // <Foo> is optional - compiler can infer
    }
}
Theodor answered 5/11, 2014 at 14:6 Comment(0)
S
1

F<int> is a method which would be assignable to a Func<int, int> variable.

F<double> is a different method which would be assignable to a Func<double, double> variable.

But you cannot have a generic variables. C# just doesn't work like that.

The closest thing to generic variables is a field of a generic class:

static class DelegateContainer<T>
{
  public static Func<T, T> FuncF = F;
}
Sabulous answered 5/11, 2014 at 13:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.