Parameter Types in C# Language

The default behavior of passing the parameter can be modified using the following parameter arguments (also called as method parameter keywords).

1. Out Parameter

It can be used to receive more than one processed output from a method. May not be initialized before the the method call. should be assigned with a value inside the method. 'out' keyword must be specified in both method call and methos declaration. method call can not pass direct values, this will result in compiler error.

2. Ref Parameter

It can be used to pass variables by reference. Should be initialized before the method call. 'ref' keyword must be specified in both method call and method definition. Method call can not pass direct values, this will result in compile time error.

3. Params Parameter

It can be used to pass zero or more values to a single parameter. Should be the last parameter in the parameter list. A method can have only one params parameter. It must be a single dimension array. The 'params' keyword must be specified only in the method definition.
 
Example

ParameterTypes.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace MyTestConsole.DemoClasses

{

    public class ParameterTypes

    {

        #region Types Of Parameter

 

        public int num = 10;

        //out parameter

        /* Method Definition */

        public int outParameter(out int numVal)

        {

            numVal = num + 10;

            return numVal;

        }

        //ref parameter

        /* Method Definition */

        public int refParameter(ref int n)

        {

            return num + n;

        }

        //params parameter

        /* Method Definition */

        public void paramsParameter(int n, params int[] lst)

        {

            for (int i = 0; i < n; i++)

            {

                Console.WriteLine(lst[i]);

            }

        }

        #endregion

    }

}

 

using MyTestConsole.DemoClasses;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.IO;

using System.Threading;

using System.Globalization;

 

namespace MyTestConsole

{

    class Program

    {

        static void Main()

        {

            Console.WriteLine("!! My First Console App !!");

            Console.WriteLine();

 

            #region Parameter Types

            //out can not take values

            int outVal;

            ParameterTypes pt = new ParameterTypes();

            Console.WriteLine("*** out Parameter ***");

            var a = pt.outParameter(out outVal); /* Method Call */

            Console.WriteLine("{0}", a);

 

            //ref first initialize

            var refVal = 20;

            Console.WriteLine();

            Console.WriteLine("*** ref Parameter ***");

            var b = pt.refParameter(ref refVal); /* Method Call */

            Console.WriteLine("{0}", b);

 

            ////params method definition only, method can have only one params           //para, single dimentional

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

            var n = lst.Length;

            Console.WriteLine();

            Console.WriteLine("*** params Parameter ***");

            pt.paramsParameter(n, lst);

 

            #endregion

            Console.WriteLine();

            Console.WriteLine("!! Finish !!");

            Console.ReadKey();

        }

    }

}
 
Output

Image-1.jpg