LogIn
I don't have account.

C# Method or Function call by value

DevSniper
119 Views

  • it pass a copy of original value in the method rather than reference
  • Any changes in this type of parameter will not reflect on parent or caller method variables.
  • Passing a Value Type parameter to a method call by value
  • using System;
    
    class FunClass
    {
        public void fun(int num)
        {
            Console.WriteLine($"\tBefore modifing num : {num}");
            num+=100;
            Console.WriteLine($"\tAfter modifing num : {num}");
        }
    }
    class Program
    {
        static void Main()
        {
            var obj = new FunClass();
            int num=15;
            Console.WriteLine($"Before calling fun num : {num}");
            obj.fun(num);
            Console.WriteLine($"After calling fun num : {num}");
        }
    }
    Before calling fun num : 15
    	Before modifing num : 15
    	After modifing num : 115
    After calling fun num : 15