C# Call By Value

Introduction

In C#, when you pass a parameter to a method, it can be done in two ways: call by value and call by reference. Call by value is the default method of passing parameters to functions in C#. When a parameter is passed by value, a copy of the actual parameter’s value is made in memory and passed to the called method. This means changes made to the parameter inside the function do not affect the original value outside the function.

How Call By Value Works

When you pass parameters by value:

  • The called method receives a copy of the original value.
  • Changes made to the parameter inside the method do not affect the original value.

Syntax

void MethodName(dataType parameter)
{
    // Code to execute
}

Example

Here’s a complete example demonstrating call by value in C#:

using System;

namespace CallByValueExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 10;
            Console.WriteLine("Before calling ChangeValue: " + number);

            // Call ChangeValue method with number as argument
            ChangeValue(number);

            Console.WriteLine("After calling ChangeValue: " + number);
        }

        // Method that attempts to change the value of the parameter
        static void ChangeValue(int value)
        {
            value = 20; // This change will not affect the original variable
            Console.WriteLine("Inside ChangeValue: " + value);
        }
    }
}

Output

Before calling ChangeValue: 10
Inside ChangeValue: 20
After calling ChangeValue: 10

Explanation

  1. Original Value: int number = 10; initializes the number variable to 10.
  2. Method Call: ChangeValue(number); calls the ChangeValue method with number as the argument.
  3. Inside Method: value = 20; changes the value of the parameter value inside the method.
  4. Output: The change inside the method does not affect the original variable number.

Example: Call By Value with Strings

Strings in C# are immutable, meaning their value cannot be changed once they are created. However, you can still see how call by value works with string variables:

using System;

namespace CallByValueStringExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string text = "Hello";
            Console.WriteLine("Before calling ChangeText: " + text);

            // Call ChangeText method with text as argument
            ChangeText(text);

            Console.WriteLine("After calling ChangeText: " + text);
        }

        // Method that attempts to change the value of the parameter
        static void ChangeText(string value)
        {
            value = "Goodbye"; // This change will not affect the original variable
            Console.WriteLine("Inside ChangeText: " + value);
        }
    }
}

Output

Before calling ChangeText: Hello
Inside ChangeText: Goodbye
After calling ChangeText: Hello

Explanation

  1. Original Value: string text = "Hello"; initializes the text variable to "Hello".
  2. Method Call: ChangeText(text); calls the ChangeText method with text as the argument.
  3. Inside Method: value = "Goodbye"; changes the value of the parameter value inside the method.
  4. Output: The change inside the method does not affect the original variable text.

Example: Call By Value with Arrays

Arrays in C# are reference types, but when you pass an array by value, it behaves differently compared to value types. You can still modify the elements of the array:

using System;

namespace CallByValueArrayExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = { 1, 2, 3 };
            Console.WriteLine("Before calling ChangeArray: " + string.Join(", ", numbers));

            // Call ChangeArray method with numbers as argument
            ChangeArray(numbers);

            Console.WriteLine("After calling ChangeArray: " + string.Join(", ", numbers));
        }

        // Method that changes the elements of the array
        static void ChangeArray(int[] values)
        {
            values[0] = 10; // This change will affect the original array
            Console.WriteLine("Inside ChangeArray: " + string.Join(", ", values));
        }
    }
}

Output

Before calling ChangeArray: 1, 2, 3
Inside ChangeArray: 10, 2, 3
After calling ChangeArray: 10, 2, 3

Explanation

  1. Original Array: int[] numbers = { 1, 2, 3 }; initializes the numbers array.
  2. Method Call: ChangeArray(numbers); calls the ChangeArray method with numbers as the argument.
  3. Inside Method: values[0] = 10; changes the first element of the array inside the method.
  4. Output: The change inside the method affects the original array numbers.

Conclusion

Call by value in C# means that a copy of the actual parameter’s value is passed to the function. Changes made to the parameter inside the function do not affect the original value outside the function. This is the default way of passing parameters in C#. Understanding call by value helps in knowing how data is handled when passed to methods, ensuring you can predict and control how changes in methods impact your data.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top