1

I want to create a Tuple using Tuple.Create() with the type signature of Tuple<String,String,Func<String,Control>>

, but when I do; I get the error:

The type arguments for method 'Tuple.Create<T1,T2,T3>(T1,T2,T3)' 
cannot be inferred from the usage. Try specifying the types explicitly.

Here's my code snippet:

public List<Tuple<String, String, Func<string,Control>>> Headers { get; set; } = new List<Tuple<String, String, Func<string,Control>>> {
            Tuple.Create("Name","Type", TypeControl),
            Tuple.Create("Age","TypeAge", AgeControl),
        };

public Control TypeControl(string data = ""){
 // code returns a Control
}
public Control AgeControl(string data = ""){
 // code returns a Control
}

I want to do this using Tuple.Create() is it possible without new Tuple<T1,T2,T3>(T1,T1,T3)

Jonathan Portorreal
  • 2,730
  • 4
  • 21
  • 38

1 Answers1

1

You have to explicitly specify the last parameter's type by either providing the type parameters:

public List<Tuple<string, string, Func<string, Control>>> Headers { get; set; } = new List<Tuple<string, string, Func<string, Control>>> {
    Tuple.Create<string, string, Func<string, Control>>("Name","Type", TypeControl),
    Tuple.Create<string, string, Func<string, Control>>("Age","TypeAge", AgeControl)
};

Or by passing a Func<string, Control>:

public List<Tuple<string, string, Func<string, Control>>> Headers { get; set; } = new List<Tuple<string, string, Func<string, Control>>> {
    Tuple.Create("Name","Type", new Func<string, Control>(TypeControl)),
    Tuple.Create("Age","TypeAge", new Func<string, Control>(AgeControl))
};

More information about the why:

Why can't C# compiler infer generic-type delegate from function signature?

Szabolcs Dézsi
  • 8,743
  • 21
  • 29