How can I swap two values of an array in C#?

.net, arrays, c#, sorting, swap

Solution

Use a tuple:

int[] arr = { 1, 2, 3 };
(arr[0], arr[1]) = (arr[1], arr[0]);
Console.WriteLine(string.Format($"{arr[0]} {arr[1]} {arr[2]}")); // 2 1 3

Tuples are available in C# 7.0. See Tuple types (C# reference).

Problem

I have an array of int containing some values starting from index 0. I want to swap two values, for example, the value of index 0 should be swapped with the value of index 1. How can I do this in the C# array?

Original source