加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 在这种情况下,有比GOTO更好的方法吗?

发布时间:2020-12-15 17:40:10 所属栏目:百科 来源:网络整理
导读:让我们以一点点诚实开始吧.我不是goto的粉丝,但我也不是一遍又一遍地重复我的代码的粉丝.无论如何,我遇到了这种情况.这是不寻常的,但不是出于这个世界.我不禁想知道这是否适合goto.有没有更好的办法? public Task Process(CancellationTokenSource token){
让我们以一点点诚实开始吧.我不是goto的粉丝,但我也不是一遍又一遍地重复我的代码的粉丝.无论如何,我遇到了这种情况.这是不寻常的,但不是出于这个世界.我不禁想知道这是否适合goto.有没有更好的办法?
public Task Process(CancellationTokenSource token)
{
    await SpinUpServiceAsync();
    foreach (var item in LongList())
    {
        if (token.IsCancellationRequested) goto cancel;
        await LongTask1(item);

        if (token.IsCancellationRequested) goto cancel;
        await LongTask2(item);

        if (token.IsCancellationRequested) goto cancel;
        await LongTask3(item);

        if (token.IsCancellationRequested) goto cancel;
        await LongTask4(item);

        if (token.IsCancellationRequested) goto cancel;
        await LongTask5(item);

        continue;
        cancel:
        {
            Log($"Cancelled during {item}");
            await SpinDownServiceAsync();
            return;
        }
    }
}

这是我看到的下一个最佳选择:

public Task Process(CancellationTokenSource token)
{
    await SpinUpServiceAsync();
    foreach (var item in LongList())
    {
        if (token.IsCancellationRequested)
        {
            Log($"Cancelled during {item}");
            await SpinDownServiceAsync();
            return;
        }
        await LongTask1(item);

        if (token.IsCancellationRequested)
        {
            Log($"Cancelled during {item}");
            await SpinDownServiceAsync();
            return;
        }
        await LongTask2(item);

        if (token.IsCancellationRequested)
        {
            Log($"Cancelled during {item}");
            await SpinDownServiceAsync();
            return;
        }
        await LongTask3(item);

        if (token.IsCancellationRequested)
        {
            Log($"Cancelled during {item}");
            await SpinDownServiceAsync();
            return;
        }
        await LongTask4(item);

        if (token.IsCancellationRequested)
        {
            Log($"Cancelled during {item}");
            await SpinDownServiceAsync();
            return;
        }
        await LongTask5(item);
    }
}

但看看那不必要的重复.

谢谢你的考虑.

解决方法

我没有LongTask1-5的确切类型,但我会这样做.
public Task Process(CancellationTokenSource token)
{
    SOMETYPE[] tasks = new SOMETYPE[] {LongTask1,LongTask2,LongTask3,LongTask4,LongTask5};
    await SpinUpServiceAsync();
    foreach (var item in LongList())
    {

        foreach(var task in tasks){
            if (token.IsCancellationRequested) {
                Log($"Cancelled during {item}");
                await SpinDownServiceAsync();

                return;
            }
            await task(item);
        }



    }
}

这种方法将任务放在一个数组中.如果需求发生变化,需要更多任务,那么数组定义会变长,但其余代码保持不变.好的代码使用循环来执行重复性任务,而不是像原始帖子中那样的大量复制和粘贴.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读