【问题标题】:How not to repeat a statement that is repeating in my method in C#?如何不重复在我的 C# 方法中重复的语句?
【发布时间】:2014-09-22 06:24:16
【问题描述】:

我讨厌在下面的函数中重复这一行:

public void Print()
{
   verticalPosition += bodyFontHeight;
   PrintData(something);

   verticalPosition += bodyFontHeight;
   PrintData(something else);

   verticalPosition += bodyFontHeight;
   PrintData(something else else);
}

我想写一个inline function 就像C/C++ 一样,我的第一次尝试是Func 代表。

但不幸的是Func 不允许使用refout 参数。

有什么想法在这种情况下应该做什么?

【问题讨论】:

  • 使用for loop怎么样?
  • @Yuriy:哎呀。我的错。想得太简单了。

标签: c# inline func


【解决方案1】:

你可以把你的东西放在你想要的任何收藏中

things = {something,something else, something else else};

然后,循环遍历它

public void Print()
{
   foreach (thing in things){
      verticalPosition += bodyFontHeight;
      PrintData(thing);
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-12
    • 2023-04-07
    • 2021-09-12
    • 2015-03-01
    相关资源
    最近更新 更多