如何在循环中使用Func或Action而不是局部变量?

时间:2018-10-08 02:35:08

标签: c# func

我正在重构一些旧代码,并且有以下循环:

def add_grade2(student, assignment_name, grade):
  for s in student_list:
    if s['first_name'] == student:
        if (assignment_name, grade) in s['assignments']:
          print(True)
        elif (assignment_name, 0) in s['assignments']:
          print('That assignment has a different grade. ')
          break
        elif (assignment_name, 1) in s['assignments']:
          print('That assignment has a different grade. ')
          break
        elif (assignment_name, 2) in s['assignments']:
          print('That assignment has a different grade. ')
          break
        elif (assignment_name, 3) in s['assignments']:
          print('That assignment has a different grade. ')
          break
        elif (assignment_name, 4) in s['assignments']:
          print('That assignment has a different grade. ')
          break
        else:
          s['assignments'].append((assignment_name, grade))
          print(False)


add_grade2('Alice', 'assignment_1', 3)
print(student_list)

我想将while (true) { if (stopLoop.IsStopRequested || ct.IsCancellationRequested) { break; } if (worker.IsPaused) { Thread.Sleep(delay); continue; } if (wait) { Thread.Sleep(delay); wait= false; continue; } //much more code here... } isPaused合并为一个块,并正在考虑使用wait

在这种特殊情况下这有意义吗?这样的Func循环会如何?

1 个答案:

答案 0 :(得分:3)

可以用一个if条件替换它吗?

if (wait || worker.IsPaused)
{
    Thread.Sleep(delay);
    wait = false;
    continue;
}

如果waittrue,则始终将其设置为false。如果是false,则将其再次设置为false