diff --git a/src/Z.Collections/System.Collections.Generic.ICollection[T]/ICollection[T].Swap.cs b/src/Z.Collections/System.Collections.Generic.ICollection[T]/ICollection[T].Swap.cs new file mode 100644 index 00000000..f7f3facd --- /dev/null +++ b/src/Z.Collections/System.Collections.Generic.ICollection[T]/ICollection[T].Swap.cs @@ -0,0 +1,29 @@ +// Description: C# Extension Methods Library to enhances the .NET Framework by adding hundreds of new methods. It drastically increases developers productivity and code readability. Support C# and VB.NET +// Website & Documentation: https://github.com/zzzprojects/Z.ExtensionMethods +// Forum: https://github.com/zzzprojects/Z.ExtensionMethods/issues +// License: https://github.com/zzzprojects/Z.ExtensionMethods/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +using System.Collections.Generic; + +public static partial class Extensions +{ + /// + /// An ICollection<T> extension method that swaps value only when it exists in a collection. + /// + /// Generic type parameter. + /// The @this to act on. + /// The old value. + /// The new value. + /// + /// true if it succeeds, false if it fails. + /// + public static void Swap(this ICollection @this, T oldValue, T newValue) + { + if (@this.Contains(oldValue)) + { + @this.Remove(oldValue); + @this.Add(newValue); + } + } +} \ No newline at end of file diff --git a/src/Z.Collections/System.Collections.Generic.IList[T]/IList[T].Swap.cs b/src/Z.Collections/System.Collections.Generic.IList[T]/IList[T].Swap.cs new file mode 100644 index 00000000..0cd404ba --- /dev/null +++ b/src/Z.Collections/System.Collections.Generic.IList[T]/IList[T].Swap.cs @@ -0,0 +1,32 @@ +// Description: C# Extension Methods Library to enhances the .NET Framework by adding hundreds of new methods. It drastically increases developers productivity and code readability. Support C# and VB.NET +// Website & Documentation: https://github.com/zzzprojects/Z.ExtensionMethods +// Forum: https://github.com/zzzprojects/Z.ExtensionMethods/issues +// License: https://github.com/zzzprojects/Z.ExtensionMethods/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.Generic; + +public static partial class Extensions +{ + /// + /// An ICollection<T> extension method that swaps item only when it exists in a collection. + /// + /// Generic type parameter. + /// The @this to act on. + /// The old value. + /// The new value. + /// + /// true if it succeeds, false if it fails. + /// + public static void Swap(this IList @this, T oldValue, T newValue) + { + var oldIndex = @this.IndexOf(oldValue); + while (oldIndex > 0) + { + @this.RemoveAt(oldIndex); + @this.Insert(oldIndex, newValue); + oldIndex = @this.IndexOf(oldValue); + } + } +} \ No newline at end of file diff --git a/src/Z.Collections/Z.Collections.csproj b/src/Z.Collections/Z.Collections.csproj index da9e6830..ccd0c9d0 100644 --- a/src/Z.Collections/Z.Collections.csproj +++ b/src/Z.Collections/Z.Collections.csproj @@ -63,6 +63,7 @@ + @@ -102,6 +103,7 @@ +