簡體   English   中英

Foreach循環多個動作

[英]Foreach loop multiple actions

for (int b = 1; b <= list.count; b++ )
{
    method1
    method2
    method3
    method4
}

我希望能夠在for循環上運行多個方法,但是我需要該語句在達到計數后立即中斷。 目前,b可能是24(列表26),但是仍然有4種方法要運行,最終將在28處運行。

聲明方法列表:

List<Action> methods = new List<Action>()
{
    method1, method2, method3, method4
};

然后使用LINQ進行迭代:

Enumerable.Range(0, list.count)
          .ToList()
          .ForEach(i => (methods[i % methods.Count])());

或者,如果您不喜歡LINQ,只需:

for (int i = 0; i < list.count; ++i)
{
    methods[i % methods.Count]();
}

注意if (b <= list.count)丑陋且if (b <= list.count)維護, 重復...

簡單的解決方案:D

for (int b = 1; b <= list.count; b++ )
{
    if (b <= list.count) method1();
    if (b <= list.count) method2();
    if (b <= list.count) method3();
    if (b <= list.count) method4();
}

如果在for循環中增加b,則檢查並在兩者之間中斷循環。 喜歡 :

if (b >= list.count) break;

此示例中查看動態調用方法。
在循環中只需要一個方法調用,就可以定義以這種方式從循環中調用任何方法的總數。 (此示例可作為新的控制台應用程序運行):

class Program
{
    static void Main(string[] args)
    {
        int numberOfMethodCalls = 5;
        string[] charList = new string[] { "A", "B", "C" };

        Console.WriteLine("Static calls --------------");
        for(int b = 0; b < numberOfMethodCalls; b++)
        {
            typeof(Program).GetMethod("Method" + (b%3).ToString()).Invoke(null, new object[] { b }); // call static method
        }

        Console.WriteLine("Calls for object ----------");
        Program p = new Program();
        for(int b = 0; b < numberOfMethodCalls; b++)
        {
            CallMethod(p.GetType().ToString(), "Method" + charList[b%3], b);
        }

        Console.ReadLine();
    }

    public static void CallMethod(string typeName, string methodName, int param)
    {
        Type type = Type.GetType(typeName);
        object instance = Activator.CreateInstance(type);
        MethodInfo methodInfo = type.GetMethod(methodName);
        methodInfo.Invoke(instance, new object[] { param });
    }

    public static void Method0(int num) { Console.WriteLine("1: STATIC b=" + num.ToString()); }
    public static void Method1(int num) { Console.WriteLine("2: STATIC b=" + num.ToString()); }
    public static void Method2(int num) { Console.WriteLine("3: STATIC b=" + num.ToString()); }

    public void MethodA(int num) { Console.WriteLine("1: b=" + num.ToString()); }
    public void MethodB(int num) { Console.WriteLine("2: b=" + num.ToString()); }
    public void MethodC(int num) { Console.WriteLine("3: b=" + num.ToString()); }
}

調用后,每種類型將獲得5個方法的校准。
控制台結果:

Static calls -----------------
1: STATIC b=0
2: STATIC b=1
3: STATIC b=2
1: STATIC b=3
2: STATIC b=4
Calls for object -------------
1: b=0
2: b=1
3: b=2
1: b=3
2: b=4

因此,您無需枚舉所有方法,可以將它們動態地定義為字符串。

for (int b = 1; b <= list.count;b++ )
{
   if (b <= list.count) method1();
   b++;
   if (b <= list.count) method2();
   b++;
   if (b <= list.count) method3();
   b++;
   if (b <= list.count) method4();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM