委托行动不需要3个参数

时间:2014-05-12 09:32:48

标签: c#

Action showTasks = (String name, String gender, String id) =>
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("Name: " + name);
            stringBuilder.Append(System.Environment.NewLine);
            stringBuilder.Append("Gender: " + gender);
            stringBuilder.Append(System.Environment.NewLine);
            stringBuilder.Append("Id: " + id);

            var builderGoogle = new AlertDialog.Builder (this);
            builderGoogle.SetTitle ("Logged in");
            builderGoogle.SetMessage (stringBuilder.ToString());
            builderGoogle.SetPositiveButton ("Ok", (o, e) => { });
            builderGoogle.Create().Show();
        };

如何传递3个输入参数来委派动作?

3 个答案:

答案 0 :(得分:6)

使用Action<string, string, string>代替Action

记住 - 有一组Action名代表。所有这些委托都封装了不返回值的方法(即具有void返回类型)。但是void方法具有不同数量的输入参数。因此,有17个不同的Action委托,它们使用不同数量的输入参数(从0到16)封装方法;

Action
Action<T1>
Action<T1,T2>
Action<T1,T2,T3>
...
Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>

所以,你应该选择合适的代表

答案 1 :(得分:4)

使用Action<T1, T2, T3>代理:

Action<String, String, String> showTasks = (String name, String gender, String id) =>

答案 2 :(得分:3)

更改类型:

Action<string, string, string> showTasks = ...