Exercise
Function Swap Reference Parameters
Objective
Write a C# function named "Swap" to swap the values of two integer numbers, which are passed by reference.
An example of use might be:
int x=5, y=3;
Swap(ref x, ref y);
Console.WriteLine("x={0}, y={1}", x, y);
(which should write "x=3, y=5")
Write Your C# Exercise
C# Exercise Example
// Importing the System namespace to access basic system functions
using System;
class Program
{
// Function to swap the values of two integers using reference parameters
public static void Swap(ref int a, ref int b)
{
// Temporary variable to hold the value of 'a'
int temp = a;
// Swap the values of 'a' and 'b'
a = b;
b = temp;
}
// Main method where the Swap function is called
public static void Main()
{
// Initialize two integer variables
int x = 5, y = 3;
// Call the Swap function with x and y passed by reference
Swap(ref x, ref y);
// Display the swapped values of x and y
Console.WriteLine("x={0}, y={1}", x, y);
}
}