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

后端 未结 6 511
情话喂你
情话喂你 2021-01-17 06:45

I have an array of int containing some values starting from index 0. I want to swap two values for example value of index 0 should be swapped with the value of index 1. How

6条回答
  •  清酒与你
    2021-01-17 07:17

    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
    

提交回复
热议问题