CS Call By Value

Value-type parameters in C#, pass a copy of the original value to the function instead of reference. The original value is not modified. It does not modify the initial value.
The real value is not altered if the passed value changes. We have a value passed during function call in the following Example.

 

 

Example:


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

 

Ouput:

Value before calling the function 20
Value inside the Display function 400
Value after calling the function 20