简体   繁体   中英

Create Tuple with Multi Parameter Func

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)

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM