C# Pass by Value vs Pass by Reference

In C#, the way arguments are passed to methods can significantly affect how those methods interact with the data. There are two main methods of passing arguments: Pass by Value and Pass by Reference.

1. Pass by Value

When an argument is passed by value, a copy of the variable is made. This means that changes made to the parameter inside the method do not affect the original variable.

- Behavior: Only the value of the argument is passed. Modifications to the parameter inside the method do not impact the original argument.

Example:
using System;

class Program
{
    static void Main()
    {
        int number = 10;
        Console.WriteLine("Before method call: " + number); // Output: 10
        ModifyValue(number);
        Console.WriteLine("After method call: " + number); // Output: 10
    }

    static void ModifyValue(int num)
    {
        num += 5; // Modifies the local copy
        Console.WriteLine("Inside method: " + num); // Output: 15
    }
}

Output:
Before method call: 10
Inside method: 15
After method call: 10

2. Pass by Reference

When an argument is passed by reference, a reference to the original variable is passed to the method. Changes made to the parameter inside the method affect the original variable.

- Behavior: A reference to the original variable is passed. Modifications to the parameter inside the method directly affect the original argument.

To pass by reference, use the `ref` or `out` keywords.

Example of Pass by Reference using `ref`:
using System;

class Program
{
    static void Main()
    {
        int number = 10;
        Console.WriteLine("Before method call: " + number); // Output: 10
        ModifyReference(ref number);
        Console.WriteLine("After method call: " + number); // Output: 15
    }

    static void ModifyReference(ref int num)
    {
        num += 5; // Modifies the original variable
        Console.WriteLine("Inside method: " + num); // Output: 15
    }
}

Output:
Before method call: 10
Inside method: 15
After method call: 15


Example of Pass by Reference using `out`:
using System;

class Program
{
    static void Main()
    {
        int result;
        Calculate(out result);
        Console.WriteLine("Result from method: " + result); // Output: 25
    }

    static void Calculate(out int num)
    {
        num = 25; // Must assign a value before using
        Console.WriteLine("Inside method: " + num); // Output: 25
    }
}

Output:
Inside method: 25
Result from method: 25

3. Summary

- Pass by Value:
- A copy of the variable is passed.
- Changes to the parameter do not affect the original variable.

- Pass by Reference:
- A reference to the original variable is passed.
- Changes to the parameter do affect the original variable.

Understanding these concepts is crucial for managing data effectively in C# methods and ensuring that your program behaves as expected.

Previous: C# Methods | Next: C# Object Oriented Programming - Class and Interface

<
>