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

后端 未结 6 604
情话喂你
情话喂你 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:41

    static void SwapInts(int[] array, int position1, int position2)
    {      
        int temp = array[position1]; // Copy the first position's element
        array[position1] = array[position2]; // Assign to the second element
        array[position2] = temp; // Assign to the first element
    }
    

    call this function and print elemet

提交回复
热议问题