diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows/SwitchBuilder.cs b/dotnet/src/Microsoft.Agents.AI.Workflows/SwitchBuilder.cs index c347ca84f3..286329b6ec 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows/SwitchBuilder.cs +++ b/dotnet/src/Microsoft.Agents.AI.Workflows/SwitchBuilder.cs @@ -37,13 +37,9 @@ public sealed class SwitchBuilder HashSet indicies = []; - int executorIndex = 0; - foreach (ExecutorBinding? executor in executors) + foreach (ExecutorBinding executor in executors) { - if (executor is null) - { - throw new ArgumentNullException(nameof(executors), $"Executors collection contains a null element at index {executorIndex}."); - } + Throw.IfNull(executor, nameof(executors)); if (!this._executorIndicies.TryGetValue(executor.Id, out int index)) { @@ -53,7 +49,6 @@ public sealed class SwitchBuilder } indicies.Add(index); - executorIndex++; } Func casePredicate = WorkflowBuilder.CreateConditionFunc(predicate)!; @@ -71,13 +66,9 @@ public sealed class SwitchBuilder { Throw.IfNull(executors); - int executorIndex = 0; - foreach (ExecutorBinding? executor in executors) + foreach (ExecutorBinding executor in executors) { - if (executor is null) - { - throw new ArgumentNullException(nameof(executors), $"Executors collection contains a null element at index {executorIndex}."); - } + Throw.IfNull(executor, nameof(executors)); if (!this._executorIndicies.TryGetValue(executor.Id, out int index)) { @@ -87,7 +78,6 @@ public sealed class SwitchBuilder } this._defaultIndicies.Add(index); - executorIndex++; } return this; diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowBuilderExtensions.cs b/dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowBuilderExtensions.cs index e7f68e4f69..86699f4683 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowBuilderExtensions.cs +++ b/dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowBuilderExtensions.cs @@ -127,27 +127,15 @@ public static class WorkflowBuilderExtensions { Throw.IfNull(targets); - List targetList = []; - using IEnumerator targetEnumerator = targets.GetEnumerator(); - if (!targetEnumerator.MoveNext()) + List targetList = new(); + foreach (ExecutorBinding target in targets) { - throw new ArgumentException("Targets collection cannot be empty.", nameof(targets)); - } - - int targetIndex = 0; - do - { - ExecutorBinding? target = targetEnumerator.Current; - if (target is null) - { - throw new ArgumentNullException(nameof(targets), $"Targets collection contains a null element at index {targetIndex}."); - } + Throw.IfNull(target, nameof(targets)); targetList.Add(target); - targetIndex++; } - while (targetEnumerator.MoveNext()); + Throw.IfNullOrEmpty(targetList, nameof(targets)); return targetList; }