How to write a generic swap function in C#

1 Answer

0 votes
using System;

/*
 * generic_swap
 * ------------
 * A fully generic C# swap function implemented using generics.
 *
 * Parameters:
 *   a, b  - variables passed by reference so their values can be swapped
 *
 * This function uses a temporary variable and assignment, which is the
 * idiomatic and type‑safe way to swap objects in C#. It works for:
 *   - built‑in types (int, double, etc.)
 *   - string
 *   - structs/classes
 *   - containers
 *   - user‑defined types with a valid swap operation
 */
static class SwapUtils
{
    public static void GenericSwap<T>(ref T a, ref T b) {
        T temp = a;
        a = b;
        b = temp;
    }
}

/*
 * Print arrays of integers
 */
static class Utils
{
    public static void PrintIntArray(int[] arr) {
        foreach (int v in arr)
            Console.Write(v + " ");
        Console.WriteLine();
    }
}

/*
 * C# equivalent of the C++ struct Point
 */
struct Point
{
    public int X;
    public int Y;

    public Point(int x, int y) {
        X = x;
        Y = y;
    }

    public override string ToString() => $"({X},{Y})";
}

class Program
{
    static void Main()
    {
        Console.WriteLine("=== TEST 1: Swap integers ===");
        int x = 10, y = 20;
        Console.WriteLine($"Before: x={x}, y={y}");
        SwapUtils.GenericSwap(ref x, ref y);
        Console.WriteLine($"After:  x={x}, y={y}\n");

        Console.WriteLine("=== TEST 2: Swap doubles ===");
        double a = 3.14, b = 2.71;
        Console.WriteLine($"Before: a={a}, b={b}");
        SwapUtils.GenericSwap(ref a, ref b);
        Console.WriteLine($"After:  a={a}, b={b}\n");

        Console.WriteLine("=== TEST 3: Swap structs ===");
        Point p1 = new Point(1, 2);
        Point p2 = new Point(9, 8);
        Console.WriteLine($"Before: p1={p1}, p2={p2}");
        SwapUtils.GenericSwap(ref p1, ref p2);
        Console.WriteLine($"After:  p1={p1}, p2={p2}\n");

        Console.WriteLine("=== TEST 4: Swap array elements ===");
        int[] arr = { 1, 2, 3, 4, 5 };
        Console.Write("Before: ");
        Utils.PrintIntArray(arr);
        SwapUtils.GenericSwap(ref arr[1], ref arr[3]);
        Console.Write("After:  ");
        Utils.PrintIntArray(arr);
        Console.WriteLine();

        Console.WriteLine("=== TEST 5: Swap strings ===");
        string str1 = "Hello";
        string str2 = "World";
        Console.WriteLine($"Before: str1=\"{str1}\", str2=\"{str2}\"");
        SwapUtils.GenericSwap(ref str1, ref str2);
        Console.WriteLine($"After:  str1=\"{str1}\", str2=\"{str2}\"\n");
    }
}


/*
run:

=== TEST 1: Swap integers ===
Before: x=10, y=20
After:  x=20, y=10

=== TEST 2: Swap doubles ===
Before: a=3.14, b=2.71
After:  a=2.71, b=3.14

=== TEST 3: Swap structs ===
Before: p1=(1,2), p2=(9,8)
After:  p1=(9,8), p2=(1,2)

=== TEST 4: Swap array elements ===
Before: 1 2 3 4 5 
After:  1 4 3 2 5 

=== TEST 5: Swap strings ===
Before: str1="Hello", str2="World"
After:  str1="World", str2="Hello"

*/

 



answered 7 hours ago by avibootz
...