diff --git a/src/Avalonia.Styling/Controls/Classes.cs b/src/Avalonia.Styling/Controls/Classes.cs index 4e2783d4ec..50605661fa 100644 --- a/src/Avalonia.Styling/Controls/Classes.cs +++ b/src/Avalonia.Styling/Controls/Classes.cs @@ -2,6 +2,8 @@ using System; using System.Collections.Generic; using Avalonia.Collections; +#nullable enable + namespace Avalonia.Controls { /// @@ -90,7 +92,7 @@ namespace Avalonia.Controls } /// - /// Remvoes all non-pseudoclasses from the collection. + /// Removes all non-pseudoclasses from the collection. /// public override void Clear() { @@ -135,7 +137,7 @@ namespace Avalonia.Controls /// public override void InsertRange(int index, IEnumerable names) { - var c = new List(); + List? toInsert = null; foreach (var name in names) { @@ -143,11 +145,16 @@ namespace Avalonia.Controls if (!Contains(name)) { - c.Add(name); + toInsert ??= new List(); + + toInsert.Add(name); } } - base.InsertRange(index, c); + if (toInsert != null) + { + base.InsertRange(index, toInsert); + } } /// @@ -176,19 +183,21 @@ namespace Avalonia.Controls /// public override void RemoveAll(IEnumerable names) { - var c = new List(); + List? toRemove = null; foreach (var name in names) { ThrowIfPseudoclass(name, "removed"); - if (Contains(name)) - { - c.Add(name); - } + toRemove ??= new List(); + + toRemove.Add(name); } - base.RemoveAll(c); + if (toRemove != null) + { + base.RemoveAll(toRemove); + } } /// @@ -223,7 +232,7 @@ namespace Avalonia.Controls /// The new contents of the collection. public void Replace(IList source) { - var toRemove = new List(); + List? toRemove = null; foreach (var name in source) { @@ -234,11 +243,17 @@ namespace Avalonia.Controls { if (!name.StartsWith(":")) { + toRemove ??= new List(); + toRemove.Add(name); } } - base.RemoveAll(toRemove); + if (toRemove != null) + { + base.RemoveAll(toRemove); + } + base.AddRange(source); }