Skip to content

tonygiang/CLSS.ExtensionMethods.IList.SwapIndices

Repository files navigation

CLSS.ExtensionMethods.IList.SwapIndices

Problem

Swapping indices in a list is a common use case that usually requires 3 lines of code:

var tempVal = collection[1];
collection[1] = collection[4];
collection[4] = tempVal;

Solution

SwapIndices is an extension method that reduces this common operation to 1 line of code:

using CLSS;

collection.SwapIndices(1, 4);

SwapIndices returns the source Ilist<T> to be friendly to a functional-style call chain. The return type will be determined by the invocation syntax of SwapIndices. With an implicit type invocation, it returns an IList<T>. With an explicit type invocation, it returns the original collection type.

using CLSS;

var numbers = new int[] { 0, 1, 2, 3, 4 };
numbers.SwapIndices(1, 4); // returns IList<int>
numbers.SwapIndices<int[], int>(1, 4); // returns int[];

Note: SwapIndices works on all types implementing the IList<T> interface, including raw C# array.

This package is a part of the C# Language Syntactic Sugar suite.

About

An extension method to swap the values at 2 different indices in an IList. A part of the C# Language Syntactic Sugar suite.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages