-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIList.SwapIndices.cs
48 lines (45 loc) · 1.56 KB
/
IList.SwapIndices.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// A part of the C# Language Syntactic Sugar suite.
using System;
using System.Collections.Generic;
namespace CLSS
{
public static partial class IListSwapIndices
{
/// <summary>
/// Swaps 2 elements at the specified index positions in place.
/// </summary>
/// <typeparam name="T">The type of <see cref="IList{T}"/> to swap indices.
/// </typeparam>
/// <typeparam name="TElement">The type of the elements of
/// <paramref name="source"/>.</typeparam>
/// <param name="source">The <see cref="IList{T}"/> to swap indices.</param>
/// <param name="index1">The first index number to be swapped.</param>
/// <param name="index2">The second index number to be swapped.</param>
/// <returns>The source collection.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is
/// null.</exception>
public static T SwapIndices<T, TElement>(this T source,
int index1,
int index2)
where T : IList<TElement>
{
if (source == null) throw new ArgumentNullException("source");
var e1 = source[index1];
source[index1] = source[index2];
source[index2] = e1;
return source;
}
/// <inheritdoc cref="SwapIndices{T, TElement}(T, int, int)"/>
public static IList<TElement> SwapIndices<TElement>(
this IList<TElement> source,
int index1,
int index2)
{
if (source == null) throw new ArgumentNullException("source");
var e1 = source[index1];
source[index1] = source[index2];
source[index2] = e1;
return source;
}
}
}