diff --git a/dotnet/src/webdriver/Interactions/ActionBuilder.cs b/dotnet/src/webdriver/Interactions/ActionBuilder.cs index 077678605d346..de9ce42a66af4 100644 --- a/dotnet/src/webdriver/Interactions/ActionBuilder.cs +++ b/dotnet/src/webdriver/Interactions/ActionBuilder.cs @@ -21,6 +21,8 @@ using System.Collections.Generic; using System.Text; +#nullable enable + namespace OpenQA.Selenium.Interactions { /// @@ -29,7 +31,7 @@ namespace OpenQA.Selenium.Interactions /// public class ActionBuilder { - private Dictionary sequences = new Dictionary(); + private readonly Dictionary sequences = new Dictionary(); /// /// Adds an action to the built set of actions. Adding an action will @@ -71,7 +73,7 @@ public IList ToActionSequenceList() /// public void ClearSequences() { - this.sequences = new Dictionary(); + this.sequences.Clear(); } /// @@ -94,8 +96,7 @@ private void ProcessTick(params Interaction[] interactionsToAdd) List usedDevices = new List(); foreach (Interaction interaction in interactionsToAdd) { - InputDevice actionDevice = interaction.SourceDevice; - if (usedDevices.Contains(actionDevice)) + if (usedDevices.Contains(interaction.SourceDevice)) { throw new ArgumentException("You can only add one action per device for a single tick."); } @@ -104,8 +105,9 @@ private void ProcessTick(params Interaction[] interactionsToAdd) List unusedDevices = new List(this.sequences.Keys); foreach (Interaction interaction in interactionsToAdd) { - ActionSequence sequence = this.FindSequence(interaction.SourceDevice); + ActionSequence sequence = this.GetOrAddSequence(interaction.SourceDevice); sequence.AddAction(interaction); + unusedDevices.Remove(interaction.SourceDevice); } @@ -116,11 +118,11 @@ private void ProcessTick(params Interaction[] interactionsToAdd) } } - private ActionSequence FindSequence(InputDevice device) + private ActionSequence GetOrAddSequence(InputDevice device) { - if (this.sequences.ContainsKey(device)) + if (this.sequences.TryGetValue(device, out ActionSequence? existingSequence)) { - return this.sequences[device]; + return existingSequence; } int longestSequenceLength = 0;