Swap Two Numbers in C#

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 2 Code Examples
Variables

What you’ll learn

  • What swapping means and why a temp variable is the safest classic approach.
  • Two C# implementations: temp variable and tuple deconstruction.
  • How to swap using ref parameters in a reusable method.
  • A live preview to visualize before/after values.

Overview

Swapping exchanges the values of two variables. If num1 = 5 and num2 = 10, after swapping num1 = 10 and num2 = 5. C# also supports elegant tuple-based swaps.

Two C# programs

Temp-variable swap and tuple ref swap.

Live preview

Enter two values and see before/after output.

Interview tip

Explain temp swap first, then mention C# tuple syntax.

Prerequisites

Variables, assignment, and basic integer operations in C#.

  • You understand that assignment overwrites the current value of a variable.
  • You know how to print values with Console.WriteLine.

Understanding the concept of swapping two numbers

Swapping makes the first variable hold the old second value, and the second variable hold the old first value.

Without temporary storage (or a safe trick), one value can be lost during assignment. That is why beginners usually learn the three-step temp method first.

Why temp is safest

The temp variable preserves one original value before overwriting either variable. It is clear, safe, and works for all integer values.

C# bonus

Tuple deconstruction (a, b) = (b, a) is also safe and very readable in modern C#.

Trace on paper

Startnum1=5, num2=10
tempstores 5
Donenum1=10, num2=5

Live preview

Enter two integers and press Swap to see before/after lines.

Enter integers (negatives also supported).

Live result
Press “Swap” to see before and after values.

Algorithm

Goal: exchange the values of two variables.

Copy first into temp

temp = num1

Move second into first

num1 = num2

Restore temp into second

num2 = temp

📜 Pseudocode

Pseudocode
procedure swap(num1, num2):
  temp = num1
  num1 = num2
  num2 = temp
1

Swap using temp variable

The most readable method for beginners — recommended in interviews.

c#
using System;

class Program
{
    static void Main()
    {
        int num1 = 5;
        int num2 = 10;

        Console.WriteLine($"Before swapping: num1 = {num1}, num2 = {num2}");

        int temp = num1;
        num1 = num2;
        num2 = temp;

        Console.WriteLine($"After swapping: num1 = {num1}, num2 = {num2}");
    }
}
2

Swap using tuple deconstruction

Idiomatic C# from the reference: a reusable method with ref parameters and tuple swap.

c#
using System;

class Program
{
    static void SwapNumbers(ref int a, ref int b)
    {
        (a, b) = (b, a);
    }

    static void Main()
    {
        int num1 = 5;
        int num2 = 10;

        Console.WriteLine($"Before swapping: num1 = {num1}, num2 = {num2}");

        SwapNumbers(ref num1, ref num2);

        Console.WriteLine($"After swapping: num1 = {num1}, num2 = {num2}");
    }
}

Optimization and alternatives

Tuple swap. (num1, num2) = (num2, num1) works directly in Main without a method.

Arithmetic swap. Possible with a = a + b tricks, but can overflow — avoid in interviews unless asked.

Default choice: temp-variable or tuple swap — both are clear and safe for integers.

❓ FAQ

Temp stores one value safely so it is not lost when variables are overwritten.
Yes. Use tuple deconstruction: (a, b) = (b, a). It is idiomatic and safe for integers.
O(1), because only a fixed number of assignments run.
After swapping, values stay the same — that is correct.
Use ref when a method must modify the caller's variables directly.
Start with the temp-variable method for clarity, then mention tuple swap as a C# alternative.

🔄 Input / output examples

BeforeAfter
num1=5, num2=10num1=10, num2=5
num1=7, num2=7num1=7, num2=7
num1=-3, num2=8num1=8, num2=-3

Edge cases and pitfalls

Most mistakes come from overwriting a value too early.

Equal

Same values

If both numbers are equal, the result is unchanged — still correct.

Order

Do not start with num1 = num2

You lose the original num1 unless it is stored in temp first.

ref

Pass by reference

When using a swap method, call with ref so changes affect the caller’s variables.

⏱️ Time and space complexity

MethodTimeExtra space
Swap with temp variableO(1)O(1)
Tuple deconstruction swapO(1)O(1)

Summary

  • Best practice: temp-variable swap is safe and easy to explain.
  • C# style: tuple deconstruction (a, b) = (b, a) is idiomatic.
  • Complexity: constant time and constant extra space.
Did you know?

Swapping is a basic operation used in many sorting algorithms like bubble sort and selection sort.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful