Parameter passing in C#


Parameter passing in C#

 

Article to explain how parameter are passed in C#.

 

Parameters are means of passing values to a method.

 

There are four different ways of passing parameters to a method in C# which are as:

 

1. Value

2. Ref (reference)

3. Out (reference)

4. Params (parameter arrays)

 

Passing parameter by value

 

By default, parameters are passed by value. In this method a duplicate copy is made and sent to the called function. There are two copies of the variables. So if you change the value in the called method it won't be changed in the calling method.

 

We use this process when we want to use but don't want to change the values of the parameters passed.

 

Practical demonstration of passing parameter by value

 

using System;

 

namespace value_parameter

{

    class Program

    {

        class XX

        {

            public int sum(int a, int b)

            {

                a = a + 10;

                b = b + 20;

                return (a + b);

            }

        }

 

        static void Main(string[] args)

        {

 

// local data members have to initialized as they are not initiated with class constructor

            int a = 10, b = 20;

 

            XX obj = new XX();

            Console.WriteLine("sum of a and b  is : " + obj.sum(a, b));

            Console.WriteLine("Value of a is : " + a);

            Console.WriteLine("Value of b is : " + b);

            Console.ReadLine();

        }

    }

}

 

In the above code we changed the values of data member a and b but it is not reflected back in the calling method. As the parameters are default passed by value.

 

Passing parameter by ref

 

Passing parameters by ref uses the address of the actual parameters to the formal parameters. It requires ref keyword in front of variables to identify in both actual and formal parameters.

 

The process of ref is bidirectional i.e. we have to supply value to the formal parameters and we get back processed value.

 

We use this process when we want to use or change the values of the parameters passed.

 

Practical demonstration of passing parameter by ref

 

using System;

 

namespace ref_parameter

{

    class Program

    {

        class XX

        {

            public int sum(ref int a, ref int b)

            {

                a = a + 10;

                b = b + 20;

                return (a + b);

            }

        }

        static void Main(string[] args)

        {

            // local data members have to initialized as they are not initiated with class constructor

            int a=10 , b=20 ;

 

            XX obj = new XX();

 

            Console.WriteLine("sum of a and b  is : " + obj.sum(ref a, ref b));

            Console.WriteLine("Value of a is : " + a);

            Console.WriteLine("Value of b is : " + b);

           

 

            Console.ReadLine();

        }

    }

}

 

Passing parameter by out

 

Like reference parameters, output parameters don't create a new storage location and are passed by reference. It requires out keyword in front of variables to identify in both actual and formal parameters.

 

The process of out is unidirectional i.e. we don't have to supply value to the formal parameters but we get back processed value.

 

We use this process when we want some parameters to bring back some processed values form the called method.

 

Practical demonstration of passing parameter by out

 

using System;

 

namespace out_parameter

{

    class Program

    {

        class XX

        {

            public int sum(int a, int b, out int c, out int d)

            {

                c = a + b;

                d = c + 100;

                return (a + b);

            }

        }

        static void Main(string[] args)

        {

            // local data members have to initialized as they are not initiated with class constructor

            int a = 10, b = 20;

 

            // the out data members doesn't need to assign initial value as they are processed and brought back

            int c, d;

 

            XX obj = new XX();

 

            Console.WriteLine("sum of a and b  is : " + obj.sum(a, b, out c, out d));

            Console.WriteLine("Value of a is : " + a);

            Console.WriteLine("Value of b is : " + b);

            Console.WriteLine("Value of c is : " + c);

            Console.WriteLine("Value of d is : " + d);

 

            Console.ReadLine();

        }

    }

}

 

Passing parameter by param (parameter arrays)

 

Params parameters "params" parameter is a very useful feature in C#. It is used when we don't know the number of parameters will be passed to the called method. Param can accept multiple values or "params" should be a single dimensional or a jagged array.

 

The params keyword lets you specify a method parameter that takes an argument where the number of arguments is variable.

 

No additional parameters are permitted after the params keyword in a method declaration, and only one params keyword is permitted in a method declaration.

 

Practical demonstration of passing parameter by param

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace param_parameter

{

    class Program

    {

        class XX

        {

            public void print(params int[] numbers)

            {

                foreach(int x in numbers)

                {

                    Console.WriteLine(" " + x);

                }

            }

        }

        static void Main(string[] args)

        {

 

            int[] numbers = { 1, 2, 3, 4, 5, 6 };

            int a = 10, b = 20, c = 30, d = 40;

 

            XX obj = new XX();

 

            obj.print(a, b, c, d);

            obj.print(numbers);

 

            Console.ReadLine();

        }

    }

}

 

What is the difference between ref and out parameter modifiers?

 

      ref

      out

  • ref is a mechanism of parameter passing by reference
  • Out is also a mechanism of parameter passing by reference
  • A variable to be sent as ref parameter must be initialized.
  • A variable to be sent as out parameter don't need to be initialized
  • ref is bidirectional i.e. we have to supply value to the formal parameters and we get back processed value.

 

  • Out is a unidirectional i.e. we don't supply any value but we get back processed value.

Hope now your would have been clear with the ways of parameter passing. If you have any feedback or query, please mail me.

Up Next
    Ebook Download
    View all
    Learn
    View all