C#干净地为每个方法参数分配一个值

时间:2017-07-28 01:37:31

标签: c# .net visual-studio c#-4.0

我创建了一个函数(如下所示),将9个按钮的文本全部分配给“”(我通常会尝试使用“”而不是将某个变量赋值为null,因此我会很感激它指出我是不正确的,但我觉得我可以用一个更干净的方式将每个参数分配给一个空白字符串。

 public void resetText(ref string buttonText1, ref string buttonText2, ref string buttonText3, ref string buttonText4, ref string buttonText5, ref string buttonText6, ref string buttonText7, ref string buttonText8, ref string buttonText9)
    {
        buttonText1 = "";
        buttonText2 = "";
        buttonText3 = "";
        buttonText4 = "";
        buttonText5 = "";
        buttonText6 = "";
        buttonText7 = "";
        buttonText8 = "";
        buttonText9 = "";
    }

是否有更简洁的方法来重新分配每个参数。如果我必须在这个函数中为这个函数分配50个变量一个新值,这看起来很糟糕(我只是用按钮作为例子。)

1 个答案:

答案 0 :(得分:0)

这几乎是最好的方法。它简单易读。

如果你真的想减少字符数,你可以做这样的buttonText1 = buttonText2 = buttonText3 = ... = "";

减少代码大小的另一种方法是通过反射来实现。我不建议采用这种方式,因为它使代码过于复杂,只保存了几个字符。