Method

These are a group of statements which collectively perform an event. Let’s look at the syntax of a method definition. Access Specifier: This determines the visibility of a variable or a method from another class.

These are a group of statements which collectively perform an event. Let’s look at the syntax of a method definition.

<access specifier> <return type > < method name > (parameter / argument list)
{
     Method Body
}

Access Specifier: This determines the visibility of a variable or a method from another class.

Parameter/ arguments: These are a group of values that we send to a method as a parameter. Parameters are not mandatory we can also have methods with empty parameters. Parameters can be passed in 3 ways.

  1. Pass by value
  2. Pass by reference
  3. Pass by output

Passing Parameter by Value:

In this, we sent a copy of actual values as parameters to the method. All the operations that we perform on these copies, inside the method will not reflect on the actual values or the variables.

using System;

namespace Method
{

    class Program
    {
        public void NumberSwap (int x, int y) //Method Definition
        {
            int temp;
            temp = x;
            x = y;
            y = temp;
            Console.WriteLine("Value of First Number = {0} and value of Second Number = {1} in method", x, y);
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Number Swap");
            Console.WriteLine("Please enter one number");
            Int32.TryParse(Console.ReadLine(), out int a);
            Console.WriteLine("Please enter another number");
            int b = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Value of First Number = {0} and value of Second Number = {1} in main before passing to method", a, b);

            Program numberswapper = new Program(); //Object Creation
            numberswapper.NumberSwap(a, b); //Method Calling

            Console.WriteLine("Value of First Number = {0} and value of Second Number = {1} in main after passing to method", a, b);

            Console.ReadKey();
        }
    }
}

Passing Parameter by Reference:

In this instead of sending a copy of values, we sent the reference to a memory location. So when we modify the values, the actual values will also be modified. You must declare the parameters with the “ref” keyword to make them reference parameters. This declaration must be done at both “method definition” and in “method calling”.

using System;

namespace Method
{

    class Program
    {
        public void NumberSwap (ref int x,ref int y) //Method Definition
        {
            int temp;
            temp = x;
            x = y;
            y = temp;
            Console.WriteLine("Value of First Number = {0} and value of Second Number = {1} in method", x, y);
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Number Swap");
            Console.WriteLine("Please enter one number");
            Int32.TryParse(Console.ReadLine(), out int a);
            Console.WriteLine("Please enter another number");
            int b = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Value of First Number = {0} and value of Second Number = {1} in main before passing to method", a, b);

            Program numberswapper = new Program(); //Object Creation
            numberswapper.NumberSwap(ref a,ref b); //Method Calling

            Console.WriteLine("Value of First Number = {0} and value of Second Number = {1} in main after passing to method", a, b);

            Console.ReadKey();
        }
    }
}

Passing Parameter by Output:

Normally a return statement can return only 1 value. There would be times when you would want to get 2 values. In such kind of cases, the Output Parameter is best.

using System;

namespace Method
{

    class Program
    {
        public void NumberSwap (out int x,out int y) //Method Definition
        {
            Console.WriteLine("Please enter one number");
            x = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Please enter another number");
            y = Convert.ToInt32(Console.ReadLine());
            int temp;
            temp = x;
            x = y;
            y = temp;
            
            Console.WriteLine("Value of First Number = {0} and value of Second Number = {1} in method", x, y);
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Number Swap");
            
            int a, b;

            //Console.WriteLine("Value of First Number = {0} and value of Second Number = {1} in main before passing to method", a, b);

            Program numberswapper = new Program(); //Object Creation
            numberswapper.NumberSwap(out a,out b); //Method Calling

            Console.WriteLine("Value of First Number = {0} and value of Second Number = {1} in main after passing to method", a, b);

            Console.ReadKey();
        }
    }
}

NOTE: The below code will not work and will throw “Use of unassigned out parameter” error. Because “out” parameters require that the called method (method definition) assign a value before the method returns. Also, variable initialization need not have to be done before the passing.

using System;
//this code will not work
namespace Method
{

    class Program
    {
        public void NumberSwap (out int x,out int y) //Method Definition
        {
            int temp;
            temp = x;
            x = y;
            y = temp;
            Console.WriteLine("Value of First Number = {0} and value of Second Number = {1} in method", x, y);
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Number Swap");
            Console.WriteLine("Please enter one number");
            Int32.TryParse(Console.ReadLine(), out int a);
            Console.WriteLine("Please enter another number");
            int b = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Value of First Number = {0} and value of Second Number = {1} in main before passing to method", a, b);

            Program numberswapper = new Program(); //Object Creation
            numberswapper.NumberSwap(out a,out b); //Method Calling

            Console.WriteLine("Value of First Number = {0} and value of Second Number = {1} in main after passing to method", a, b);

            Console.ReadKey();
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *