Friday 17 August 2018

C#: Method Parameters

There are 4 different types of method parameters.
a.   Value Parameters
b.   Reference Parameters
c.   Out parameters
d.   Parameter Arrays

C# supports both pass by value and pass by reference.

Value Parameters
If you call the method by value, then the changes happened inside the method on arguments are not reflected outside the method.

Program.cs
using System;

class Program
{
    static void Main(string[] args)
    {
        int i = 10;

        Console.WriteLine("Before calling changeMe method {0}", i);

        changeMe(i);

        Console.WriteLine("After calling changeMe method {0}", i);

    }

    static void changeMe(int j)
    {
        j = 333;
    }

}


Output
Before calling changeMe method 10
After calling changeMe method 10



Reference Parameters

To pass the parameter by reference use the ‘ref’ keyword. If you call the method by reference, then the changes happened on arguments inside the method are visible outside the method also.



Example

    static void changeMe(ref int j)
    {
        j = 333;
    }

You need to use the ‘ref’ keyword while calling ‘changeMe’ method.

changeMe(ref i);

Program.cs

using System;

class Program
{
    static void Main(string[] args)
    {
        int i = 10;

        Console.WriteLine("Before calling changeMe method {0}", i);

        changeMe(ref i);

        Console.WriteLine("After calling changeMe method {0}", i);

    }

    static void changeMe(ref int j)
    {
        j = 333;
    }

}


Output
Before calling changeMe method 10
After calling changeMe method 333

Notify the output, value of the variable ‘i’ is changed to 333 after calling ‘changeMe’ method.

Out parameters
If you want a method to return more than one value, you should use ‘out’ keyword.

Example
    static void arithmetic(int i, int j, out int sum, out int sub, out int mul, out int div)
    {
        sum = i + j;
        sub = i = j;
        mul = i * j;
        div = i / j;
    }

Above method is called like below.

arithmetic(i, j, out result1, out result2, out result3, out result4);

After calling the method arithmetic, the results are stored in the variable result1, result2, result3, result4.

Program.cs
using System;

class Program
{
    static void Main(string[] args)
    {
        int i = 10;
        int j = 5;

        int result1 = 0;
        int result2 = 0;
        int result3 = 0;
        int result4 = 0;

        Console.WriteLine("Before calling arithmetic method result1 : {0}", result1);
        Console.WriteLine("Before calling arithmetic method result2 : {0}", result2);
        Console.WriteLine("Before calling arithmetic method result3 : {0}", result3);
        Console.WriteLine("Before calling arithmetic method result4 : {0}", result4);

        arithmetic(i, j, out result1, out result2, out result3, out result4);

        Console.WriteLine("After calling arithmetic method result1 : {0}", result1);
        Console.WriteLine("After calling arithmetic method result2 : {0}", result2);
        Console.WriteLine("After calling arithmetic method result3 : {0}", result3);
        Console.WriteLine("After calling arithmetic method result4 : {0}", result4);

    }

    static void arithmetic(int i, int j, out int sum, out int sub, out int mul, out int div)
    {
        sum = i + j;
        sub = i = j;
        mul = i * j;
        div = i / j;
    }

}

Output
Before calling arithmetic method result1 : 0
Before calling arithmetic method result2 : 0
Before calling arithmetic method result3 : 0
Before calling arithmetic method result4 : 0
After calling arithmetic method result1 : 15
After calling arithmetic method result2 : 5
After calling arithmetic method result3 : 25
After calling arithmetic method result4 : 1

Out Vs ref parameters
They're pretty much the same - the only difference is that a variable you pass as an out parameter doesn't need to be initialized but passing it as a ref parameter has to be set to something.

Parameter Arrays
Just like how you pass variables as arguments to a method, you can also pass an array as an argument to method.


Program.cs
using System;

class Program
{
    static void Main(string[] args)
    {
        int[] arr = new int[5];

        arr[0] = 2;
        arr[1] = 3;
        arr[2] = 5;
        arr[3] = 7;
        arr[4] = 11;

        printArray(arr);
    }

    static void printArray(int[] arr)
    {
        Console.WriteLine("Array length is {0}", arr.Length);

        foreach(int data in arr)
        {
            Console.WriteLine(data);
        }
    }
}

Output
Array length is 5
2
3
5
7
11

C# provides ‘params’ keyword, where you can call ‘printArray’ method like below.

printArray(2, 3, 5, 7, 11);

If you define method using ‘params’ keyword, then you can also call the method without passing arguments like ‘printArray()’.


Program.cs
using System;

class Program
{
    static void Main(string[] args)
    {
        printArray();
        printArray(2);
        printArray(2, 3, 5, 7, 11);
    }

    static void printArray(params int[] arr)
    {
        Console.WriteLine("Array length is {0}", arr.Length);

        foreach(int data in arr)
        {
            Console.WriteLine(data);
        }
    }
}

Output
Array length is 0
Array length is 1
2
Array length is 5
2
3
5
7
11


The parameter array should be the last argument to the method.


Previous                                                 Next                                                 Home

No comments:

Post a Comment