---
title: "What is Call By Value in C# ?"  
description: "What is Call By Value in C# ?"  
author: "Rahul Roi"  
published: 2020-11-16  
updated: 2023-06-06  
canonical: https://www.mindstick.com/interview/33754/what-is-call-by-value-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 5 minutes  

---

# What is Call By Value in C# ?

Into the C#, the value-type parameters are that pass a copy of the original value to the function rather than reference. That does not modify the original value. The change made in the passed value does not alter the actual value. Into the following example, we have a pass value during the function call.

##### Example :-

```
using System;
namespace CallByValue
{
    class Program
    {
        // User defined function
        public void Show(int val)
        {
             val *= val; // Manipulating value
            Console.WriteLine("Value inside the show function "+val);
            // No return statement
        }
        // Main function, execution entry point of the program
        static void Main(string[] args)
        {
            int val = 50;
            Program program = new Program(); // Creating Object
            Console.WriteLine("Value before calling the function "+val);
            program.Show(val); // Calling Function by passing value
            Console.WriteLine("Value after calling the function " + val);
        }
    }
}  
```

## Output :-

```
Value before calling the function 50
Value inside the show function 2500
Value after calling the function 50
```

## Answers

### Answer by Aryan Kumar

Call by value is a method of passing arguments to a function in C#. When a value is passed by value, a copy of the value is made and passed to the function. Any changes made to the value inside the function do not affect the original value.

For example, the following code shows how to pass an integer value to a function by value:

C#

```plaintext
int value = 10;

void myFunction(int value)
{
    value = value * 2;
}

myFunction(value);

Console.WriteLine(value); // Prints 10
```

In this example, the value 10 is copied to the local variable value inside the myFunction() function. When the value variable is multiplied by 2, the change only affects the local variable. The original value of 10 is not affected.

Call by value is the default way of passing arguments to functions in C#. It is a simple and efficient way of passing data to functions. However, it can sometimes be inconvenient if you want to make changes to the original value inside the function.

In these cases, you can use call by reference. Call by reference passes a reference to the value to the function. This means that any changes made to the value inside the function will affect the original value.

For example, the following code shows how to pass an integer value to a function by reference:

C#

```plaintext
int value = 10;

void myFunction(ref int value)
{
    value = value * 2;
}

myFunction(ref value);

Console.WriteLine(value); // Prints 20
```

In this example, the value variable is passed by reference to the myFunction() function. When the value variable is multiplied by 2, the change affects the original value. The value 20 is printed to the console.

Call by reference can be more convenient than call by value, but it can also be more dangerous. If you accidentally change the value of a variable inside a function, you may not realize that you have done so. This can lead to bugs in your code.

It is important to choose the right method of passing arguments to functions based on your specific needs. Call by value is a good choice for most cases, but call by reference can be useful when you need to make changes to the original value.

### Answer by Rahul Roi

Into the C#, the value-type parameters are that pass a copy of the original value to the function rather than reference. That does not modify the original value. The change made in the passed value does not alter the actual value. Into the following example, we have a pass value during the function call.

##### Example :-

```
using System;
namespace CallByValue
{
    class Program
    {
        // User defined function
        public void Show(int val)
        {
             val *= val; // Manipulating value
            Console.WriteLine("Value inside the show function "+val);
            // No return statement
        }
        // Main function, execution entry point of the program
        static void Main(string[] args)
        {
            int val = 50;
            Program program = new Program(); // Creating Object
            Console.WriteLine("Value before calling the function "+val);
            program.Show(val); // Calling Function by passing value
            Console.WriteLine("Value after calling the function " + val);
        }
    }
}  
```

## Output :-

```
Value before calling the function 50
Value inside the show function 2500
Value after calling the function 50
```


---

Original Source: https://www.mindstick.com/interview/33754/what-is-call-by-value-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
