C Sharp Call By Value

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

C# Call By Value

In C#, value-type parameters are that pass a copy of original value to the function rather than reference. It does not modify the original value. A change made in passed value does not alter the actual value. In the following example, we have pass value during function call.

C# Call By Value Example

  1. using System;  
  2. namespace CallByValue  
  3. {  
  4.     class Program  
  5.     {  
  6.         // User defined function  
  7.         public void Show(int val)  
  8.         {  
  9.              val *= val; // Manipulating value  
  10.             Console.WriteLine("Value inside the show function "+val);  
  11.             // No return statement  
  12.         }  
  13.         // Main function, execution entry point of the program  
  14.         static void Main(string[] args)  
  15.         {  
  16.             int val = 50;  
  17.             Program program = new Program(); // Creating Object  
  18.             Console.WriteLine("Value before calling the function "+val);  
  19.             program.Show(val); // Calling Function by passing value            
  20.             Console.WriteLine("Value after calling the function " + val);  
  21.         }  
  22.     }  
  23. }  

Output:

Value before calling the function 50
Value inside the show function 2500
Value after calling the function 50