Signup/Sign In

Passing Parameters to a C# Method - Pass by Value, Reference etc

Posted in Programming   LAST UPDATED: SEPTEMBER 11, 2019

    We covered all about C# Methods in our previous tutorial. In this tutorial we will learn about how to pass parameters to a method. There are three ways to pass parameters to a method.

    1. Pass by Value

    2. Pass by Reference

    3. Pass by Output

    Let's cover these one by one.




    1. Pass by Value

    Passing parameters to a method by value is simple. When a simple variable is passed as a parameter to any method, it is passed as a value.

    The following example demonstrates the concept of passing a variable by value.

    Filename: Program.cs

    using System;
    
    namespace Studytonight
    {
        class Program
        {
            public static void Area(int a)
            {
                Console.WriteLine("Adding 2 to the value of a");
                a = a+2;
                Console.WriteLine("New value of a: " + a);
                Console.WriteLine("Calculating the area of geometric shapes");
                int result = a * a;
                Console.WriteLine("Area of Square is " + result);
            }
    
            static void Main(string[] args)
            {
                int s = 18;
                Area(s);
                Console.WriteLine("Side Value is " + s);
                Console.ReadKey();
            }
        }
    }

    Output:

    Adding 2 to the value of a
    New value of a: 20
    Calculating the area of geometric shapes
    Area of Square is 400
    Side Value is 18

    In the above code, the variable s is a variable which is passed as a value to the Area method. The content of variable s is copied to the parameter a and after that whatever processing is done on parameter a or using the parameter a, will have no affect on the variable s. Hence, the side value remains 18 although we add 2 to the value of a before calculating the area.

    In case of pass by value a new storage location is allocated for each value passed as parameters.




    2. Pass by Reference

    The ref keyword indicates a value that is passed by reference. When we pass parameters by reference, unlike passing parameters by value, a new storage location is not created for these parameters. The reference parameters represent the same memory location as the actual parameters that are supplied to the method.

    In simpler words, when we pass a reference of a variable to a method, then we pass the address or the memory location where the value is stored. So if, inside the method, the value of the parameter is changed, then the actual value stored at the memory location gets changed as no copy is getting created for the value passed as parameter.

    The following example demonstrates the concept of passing a variable by reference

    Filename: Program.cs

    using System;
    
    namespace Studytonight
    {
        public class Program
        {
            public static void Area(ref int a)
            {
                Console.WriteLine("Calculating Area of Geometric Shapes");
                a = a * a;
                Console.WriteLine("Area of Square is " + a);
            }
    
            public static void Main()
            {
                int s = 20;
                Area(ref s);
                Console.WriteLine("Side Value is " + s);
            }
        }
    }

    Output:

    Calculating the area of geometric shapes
    Area of Square is 400
    Side Value is 400

    Passing the reference of the variable s to the variable a in Area method by using ref keyword implies that the variable a will then contain the reference of variable s so the changes that are made to the variable a will affect the value of variable s. Hence, pass by reference must be used very carefully.




    3. Pass by Output

    The out keyword indicates a value that is passed by reference type. It is pretty similar to the ref keyword, the only difference is that out doesn't require a variable to be initialized before we pass it as an argument to the method. However, the called method is required to assign a value to the passed reference before the method returns.

    The following example demonstrates the concept of passing a variable by reference using the out keyword.

    Filename: Program.cs

    using System;
    
    namespace Studytonight
    {
        public class Program
        {
            public static void Area(out int a)
            {
                a = 50;
                Console.WriteLine("Calculating Area of Geometric Shapes");
                a = a * a;
                Console.WriteLine("Area of Square is " + a);
            }
            public static void Main()
            {
                int s;
                Area(out s);
                Console.WriteLine("Side Value is " + s);
                Console.ReadKey();
            }
        }
    }

    Output:

    Calculating the area of geometric shapes
    Area of Square is 400
    Side Value is 400

    In the code above, we declared a variable s and passed it to the Area method using the out keyword without initializing any value to the variable s. However, the called Area method is initializing the value before it returns the result to the calling statement. And after the Area value assigns a value to the variable s, the value can still be accessed even after the function ends.

    About the author:
    Subject Matter Expert of C# Programming at Studytonight.
    Tags:C#C# TutorialC# Methods
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS