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

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

    You could create an extension method that would work for any array

    public static void SwapValues(this T[] source, long index1, long index2)
    {
        T temp = source[index1];
        source[index1] = source[index2];
        source[index2] = temp;
    }
    

提交回复
热议问题