Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// An ICollection&lt;T&gt; extension method that swaps value only when it exists in a collection.
/// </summary>
/// <typeparam name="T">Generic type parameter.</typeparam>
/// <param name="this">The @this to act on.</param>
/// <param name="oldValue">The old value.</param>
/// <param name="newValue">The new value.</param>
/// <returns>
/// true if it succeeds, false if it fails.
/// </returns>
public static void Swap<T>(this ICollection<T> @this, T oldValue, T newValue)
{
if (@this.Contains(oldValue))
{
@this.Remove(oldValue);
@this.Add(newValue);
}
}
}
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// An ICollection&lt;T&gt; extension method that swaps item only when it exists in a collection.
/// </summary>
/// <typeparam name="T">Generic type parameter.</typeparam>
/// <param name="this">The @this to act on.</param>
/// <param name="oldValue">The old value.</param>
/// <param name="newValue">The new value.</param>
/// <returns>
/// true if it succeeds, false if it fails.
/// </returns>
public static void Swap<T>(this IList<T> @this, T oldValue, T newValue)
{
var oldIndex = @this.IndexOf(oldValue);
while (oldIndex > 0)
{
@this.RemoveAt(oldIndex);
@this.Insert(oldIndex, newValue);
oldIndex = @this.IndexOf(oldValue);
}
}
}
2 changes: 2 additions & 0 deletions src/Z.Collections/Z.Collections.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="System.Collections.Generic.IList[T]\IList[T].Swap.cs" />
<Compile Include="System.Collections.Generic.ICollection[T]\ICollection[T].AddIf.cs" />
<Compile Include="System.Collections.Generic.ICollection[T]\ICollection[T].AddIfNotContains.cs" />
<Compile Include="System.Collections.Generic.ICollection[T]\ICollection[T].AddRange.cs" />
Expand Down Expand Up @@ -102,6 +103,7 @@
<Compile Include="System.Collections.IDictionary\IDictionary.ToHashtable.cs" />
<Compile Include="System.Collections.Specialized.NameValueCollection\NameValueCollection.ToDictionary.cs" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 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;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Z.Collections.Test
{
[TestClass]
public class System_Collections_Generic_IList_T_Swap
{
[TestMethod]
public void Swap()
{
// Type
var stringList = new List<string>() { "Foo", "Bar", "Baz" };
var obj1 = new { Id = 1, Message = "Foo" };
var obj2 = new { Id= 2, Message = "Bar" };
var obj3 = new { Id = 3, Message = "Baz" };

// Examples
var example1 = new List<string>(stringList);
var example2 = new List<string>(stringList);
var example3 = new List<string>(stringList);
var example4 = new List<string>(stringList);
var example5 = new List<string>() {"Foo", "Bar", "Bar"};
var example6 = new List<object>() { obj1, obj2 };

//Swaps
example1.Swap("Baz", "NewBaz");
example2.Swap("NoValueFound", "NoBaz");
example3.Swap(null, "NothingChanged");
example4.Swap("Bar", "NewBar");
example5.Swap("Bar", "Baz");
example6.Swap(obj2, obj3);

// Expected results
var result1 = new List<string>() {"Foo", "Bar", "NewBaz"};
var result2 = new List<string>(stringList);
var result3 = new List<string>(stringList);
var result4 = example4.ElementAt(1).Equals("NewBar");
var result5 = example5.Contains("Bar");
var result6 = example6.Contains(obj3);

// Unit Test
Assert.IsTrue(example1.Contains("NewBaz"), "The collection should contain this value");
Assert.IsFalse(example2.Contains("NoBaz"), "The collection shouldn't contain this value");
Assert.IsTrue(example1.SequenceEqual(result1), "The collection should have the replaced value");
Assert.IsTrue(example2.SequenceEqual(result2), "The collection shouldn't have the replaced value");
Assert.IsTrue(example3.SequenceEqual(result3), "The collection shouldn't have the replaced value");
Assert.IsTrue(result4, "The item in a collection shouldn't change it's position");
Assert.IsFalse(result5, "All values should be swaped");
Assert.IsTrue(result6, "The object in a collection should be swaped");
}
}
}
1 change: 1 addition & 0 deletions test/Z.Collections.Test/Z.Collections.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<Compile Include="System.Collections.Generic.ICollection[T]\ICollection[T].RemoveRangeIf.cs" />
<Compile Include="System.Collections.Generic.ICollection[T]\ICollection[T].RemoveRangeIfContains.cs" />
<Compile Include="System.Collections.Generic.ICollection[T]\ICollection[T].RemoveWhere.cs" />
<Compile Include="System.Collections.Generic.IList[T]\IList[T].Swap.cs" />
<Compile Include="System.Collections.Generic.IDictionary[string, object]\IDictionary[string, object].ToExpando.cs" />
<Compile Include="System.Collections.Generic.IDictionary[string, string]\IDictionary[string, string].ToNameValueCollection.cs" />
<Compile Include="System.Collections.Generic.IDictionary[TKey,TValue]\IDictionary[TKey,TValue].AddIfNotContainsKey.cs" />
Expand Down