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)

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.