How to swap two values of an array in C#

1 Answer

0 votes
using System;

class Program
{
	static void Swap<T> (ref T a, ref T b) {
        T temp = a;
        a = b;
        b = temp;
    }

    static void Main() {
        int[] arr = new int[] {99, 3, 7, 0, 2, 1, 8, 6};

		Swap(ref arr[0], ref arr[4]);

		for (int i = 0; i < arr.Length; i++) {
			Console.Write(arr[i] + " ");
		}
    }
}





/*
run:
  
2 3 7 0 99 1 8 6 
  
*/

 



answered Apr 19, 2023 by avibootz

Related questions

1 answer 154 views
154 views asked Apr 20, 2023 by avibootz
1 answer 133 views
1 answer 138 views
1 answer 127 views
1 answer 133 views
133 views asked Apr 20, 2023 by avibootz
1 answer 131 views
131 views asked Apr 19, 2023 by avibootz
1 answer 117 views
117 views asked Apr 19, 2023 by avibootz
...