Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在委托内中断循环_C#_Loops_Delegates - Fatal编程技术网

C# 在委托内中断循环

C# 在委托内中断循环,c#,loops,delegates,C#,Loops,Delegates,我创建了一个简单类,可以用来创建循环: public class SimpleLoop { public int I { get; set; } public Predicate<int> Condition { get; set; } public int Increment { get; set; } public Action<int> Action { get; set; } public SimpleLoop(int i

我创建了一个简单类,可以用来创建循环:

public class SimpleLoop
{
    public int I { get; set; }
    public Predicate<int> Condition { get; set; }
    public int Increment { get; set; }
    public Action<int> Action { get; set; }

    public SimpleLoop(int i, Predicate<int> condition, int increment, Action<int> action)
    {
        I = i;
        Condition = condition;
        Increment = increment;
        Action = action;
        Invoke();
    }

    private void Invoke()
    {
        for (int i = I; Condition.Invoke(i); i += Increment)
        {
            Action.Invoke(i);
        }
    }
}
公共类SimpleLoop
{
公共整数I{get;set;}
公共谓词条件{get;set;}
公共整数增量{get;set;}
公共操作动作{get;set;}
公共SimpleLoop(int i,谓词条件,int增量,动作)
{
I=I;
条件=条件;
增量=增量;
行动=行动;
调用();
}
私有void Invoke()
{
for(int i=i;Condition.Invoke(i);i+=Increment)
{
行动.援引(i);
}
}
}
然后我可以这样称呼这个循环:

new SimpleLoop(0, i => i <= 12, 1, delegate (int i)
{
    Console.WriteLine(i);
});

newsimpleloop(0,i=>i使用与Paralell Linq相同的方法。
您的委托不仅应该获取值,还应该获取表示迭代本身状态的对象

Action<int> action
动作
变成

Action<SimpleLoopState, int> ...


delegate (state, value) => 
{
state.Break();
}
动作。。。
委托(状态、值)=>
{
state.Break();
}

为什么不直接使用
Func
而不是
操作
,并要求学员返回
true
继续或
false
中断

例如:

public class SimpleLoop
{
    public int I { get; set; }
    public Predicate<int> Condition { get; set; }
    public int Increment { get; set; }
    public Func<int, bool> Action { get; set; }

    public SimpleLoop(int i, Predicate<int> condition, int increment, Func<int, bool> action)
    {
        I = i;
        Condition = condition;
        Increment = increment;
        Action = action;
        Invoke();
    }

    private void Invoke()
    {
        for (int i = I; Condition.Invoke(i); i += Increment)
        {
            if (!Action.Invoke(i))
                break;
        }
    }
}
公共类SimpleLoop
{
公共整数I{get;set;}
公共谓词条件{get;set;}
公共整数增量{get;set;}
公共函数操作{get;set;}
公共SimpleLop(int i,谓词条件,int增量,Func操作)
{
I=I;
条件=条件;
增量=增量;
行动=行动;
调用();
}
私有void Invoke()
{
for(int i=i;Condition.Invoke(i);i+=Increment)
{
如果(!Action.Invoke(i))
打破
}
}
}
然后:

new SimpleLoop(0, i => i <= 12, 1, delegate (int i)
{
    Console.WriteLine(i);
    return i < 5;
});

newsimpleloop(0,i=>i我不确定您试图在哪里打破循环;
我试着以9的值打破它,结果成功了。
这是密码

enter code here
   private void Invoke()
    {
        for (int i = I; Condition.Invoke(i); i += Increment)
        {
            Action.Invoke(i);
            if(i.Equals(9))
            break;
        }
    }
谢谢
Manish Prasad

我找到了一种打破循环的方法。我基于类
异常创建了一个类,并将其命名为
BreakException

public class BreakException : Exception { }
public static void Break()
{ throw new BreakException(); }
然后我添加了一个静态方法来抛出一个
BreakException

public class BreakException : Exception { }
public static void Break()
{ throw new BreakException(); }
现在我可以捕获此异常以打破循环:

private void Invoke()
{
    for (int i = I; Condition.Invoke(i); i += Increment)
    {
        try
        {
            Action.Invoke(i);
        }
        catch (BreakException)
        { break; }
    }
}
注意:如果我在
try catch
块中调用
Break
-方法,我必须确保没有捕获
BreakException
,如果捕获到一个,我必须再次调用
Break
-方法:

new SimpleLoop(0, i => i <= 12, 1, delegate (int i)
{
    try
    {
        if (i == 5)
        { throw new ArgumentException(); }//raise an other Exception 
        if (i > 10)
        { SimpleLoop.Break(); }
          Console.WriteLine(i);
    }
    catch (Exception exception)//Catching every Exception, it would be better to catch just ArgumentExceptions
    {
        if (exception.GetType() == typeof(BreakException))
        { SimpleLoop.Break(); }//Call Break again
        Console.WriteLine("Error at " + i);
    }
});
newsimpleloop(0,i=>i10)
{SimpleLoop.Break();}
控制台写入线(i);
}
catch(Exception Exception)//捕获每个异常,最好只捕获一个异常
{
if(exception.GetType()==typeof(BreakException))
{SimpleLoop.Break();}//再次调用Break
控制台写入线(“错误在”+i);
}
});

我假设这只是为了玩代码…因为它在生产代码中几乎毫无用处:\n您可以让调用的函数返回一个布尔值,说明是否继续…谢谢您的回答。我认为这是最好的解决方案,但我会使用
枚举
而不是
bool
,这样我就可以选择继续在
break
continue
和nothing i.Equals(9)与i==9是一回事,一个异常会让你损失很多性能(相对于你正在做的工作而言)。在许多项目中,对于可能被广泛使用的东西来说,这么大的性能成本是不可接受的。。。