本文介绍了如何以干净和正确的方式缩短长参数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

public static class ButtonProperties
{
    public static void _ButtonProperties(Button btn_dashboard, Button btn_products, Button btn_supplier, Button btn_customer, Button btn_sales, Button btn_inventory, Button btn_settings)
    {
        btn_dashboard.FlatStyle = FlatStyle.Flat;
        btn_dashboard.FlatAppearance.BorderSize = 0;
        btn_products.FlatStyle = FlatStyle.Flat;
        btn_products.FlatAppearance.BorderSize = 0;
        btn_supplier.FlatStyle = FlatStyle.Flat;
        btn_supplier.FlatAppearance.BorderSize = 0;
        btn_customer.FlatStyle = FlatStyle.Flat;
        btn_customer.FlatAppearance.BorderSize = 0;
        btn_sales.FlatStyle = FlatStyle.Flat;
        btn_sales.FlatAppearance.BorderSize = 0;
        btn_inventory.FlatStyle = FlatStyle.Flat;
        btn_inventory.FlatAppearance.BorderSize = 0;
        btn_settings.FlatStyle = FlatStyle.Flat;
        btn_settings.FlatAppearance.BorderSize = 0;
    }
}

如您所见,这需要太多参数,我想缩短它.

As you can see, this takes too many parameters and I'd like to make it shorter.

推荐答案

您可以使用 params-array:

You can do it with params-array:

public static class ButtonProperties
{
    public static void _ButtonProperties(params Button[] buttons)
    {
        foreach (Button b in buttons)
        {
            b.FlatStyle = FlatStyle.Flat;
            b.FlatAppearance.BorderSize = 0;
        }
    }
}

现在您可以传递单个按钮、多个(逗号分隔)甚至一个 Button[].

Now you can pass a single button, multiple(comma separated) or even a Button[].

举个例子:

ButtonProperties._ButtonProperties(btn_dashboard, btn_products);

这篇关于如何以干净和正确的方式缩短长参数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 14:56