C# Call By Value

In C#, method parameters are passed by value by default. This means that when you pass a variable as an argument to a method, a copy of the variable’s value is created and passed to the method. Any changes made to the parameter within the method do not affect the original variable in the calling code.

Here’s an example to illustrate the concept:

class Program
    static void Main(string[] args)
        int number = 10;
        Console.WriteLine("Before calling the method: " + number);
        Console.WriteLine("After calling the method: " + number);

    static void ModifyValue(int value)
        value = 20; // Modify the parameter value
        Console.WriteLine("Inside the method: " + value);

In this example, the ModifyValue method takes an int parameter value and assigns it a new value of 20. However, when you run the program, you’ll see that the value of the number variable remains unchanged. This is because the value parameter is a copy of the original variable’s value.


Before calling the method: 10
Inside the method: 20
After calling the method: 10

If you want a method to modify the original variable, you can pass it by reference using the ref or out keywords. This allows the method to access and modify the original variable itself.