Swap Two Numbers in C#
What you’ll learn
- What swapping means and why a
tempvariable is the safest classic approach. - Two C# implementations: temp variable and tuple deconstruction.
- How to swap using
refparameters 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.
Tuple deconstruction (a, b) = (b, a) is also safe and very readable in modern C#.
Trace on paper
Live preview
Enter two integers and press Swap to see before/after lines.
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
procedure swap(num1, num2):
temp = num1
num1 = num2
num2 = temp Swap using temp variable
The most readable method for beginners — recommended in interviews.
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}");
}
} Swap using tuple deconstruction
Idiomatic C# from the reference: a reusable method with ref parameters and tuple swap.
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
🔄 Input / output examples
| Before | After |
|---|---|
num1=5, num2=10 | num1=10, num2=5 |
num1=7, num2=7 | num1=7, num2=7 |
num1=-3, num2=8 | num1=8, num2=-3 |
Edge cases and pitfalls
Most mistakes come from overwriting a value too early.
Same values
If both numbers are equal, the result is unchanged — still correct.
Do not start with num1 = num2
You lose the original num1 unless it is stored in temp first.
Pass by reference
When using a swap method, call with ref so changes affect the caller’s variables.
⏱️ Time and space complexity
| Method | Time | Extra space |
|---|---|---|
| Swap with temp variable | O(1) | O(1) |
| Tuple deconstruction swap | O(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.
Swapping is a basic operation used in many sorting algorithms like bubble sort and selection sort.
8 people found this page helpful
