What are the types of parameters in C#?
By in C# on Jul 26 2006
  • Keerthi Venkatesan
    Apr, 2016 22

    There are four different ways of parameters Value , Out ,Ref , Params

    • 0
  • Jul, 2006 26

    Parameters are means: way of passing values to a method.The syntax of passing parameter in C# is:

    [modifier] DataType ParameterName

    There are four different ways of passing parameters to a method in C#. The four different types of parameters are:

    1. Value
    2. Out
    3. Ref
    4. Params

    1. Value parameters This is the default parameter type in C#. If the parameter does not have any modifier it is "value" parameter by default.When we use "value" parameters the actual value is passed to the function,which means changes made to the parameter is local to the function and is not passed back to the calling part.

    using System;
    class ParameterInCSharp
    {
           static void ParameterType(int intValueParameter)
          {
                 intValueParameter = 100;
          }
          static void Main()
          {
                  int intAnotherValue = 5;
                  ParameterType(intAnotherValue);
                  Console.WriteLine(intAnotherValue);                 
          }
    }

    Output of the above program would be 5.Eventhough the value of the parameter Param1 is changed within MyMethod it is not passed back to the calling part since value parameters are input only.

    2. Out parameters "out" parameters are output only parameters meaning they can only passback a value from a function.We create a "out" parameter by preceding the parameter data type with the out modifier. When ever a "out" parameter is passed only an unassigned reference is passed to the function.

    using System;
    class ParameterInCSharp
    {
             static void ParameterType(out int outParameter)
             {
                      outParameter = 100;
             }
             static void Main()
             {
                      int intAnotherValue = 5;
                      ParameterType(intAnotherValue);
                      Console.WriteLine(out intAnotherValue);            
             }
    }

    Output of the above program would be 100 since the value of the "out" parameter is passed back to the calling part.

    Note:

    The modifier "out" should precede the parameter being passed even in the calling part. "out" parameters cannot be used within the function before assigning a value to it. A value should be assigned to the "out" parameter before the method returns.

    3. Ref parameters "ref" parameters are input/output parameters meaning they can be used for passing a value to a function as well as to get back a value from a function.We create a "ref" parameter by preceding the parameter data type with the ref modifier. When ever a "ref" parameter is passed a reference is passed to the function.

    using System;
    class ParameterInCSharp
    {
             static void ParameterType(ref int refParameter)
             {
                      refParameter = refParameter + 10;
             }
             static void Main()
             {
                      int intAnotherValue=5;
                      ParameterType(intAnotherValue);
                      Console.WriteLine(ref intAnotherValue);             
             }
    }

    Output of the above program would be 105 since the "ref" parameter acts as both input and output.

    Note:

    The modifier "ref" should precede the parameter being passed even in the calling part. "ref" parameters should be assigned a value before using it to call a method. 4.Params parameters "params" parameters is a very useful feature in C#. "params" parameter are used when a variable number of arguments need to be passed.The "params" should be a single dimensional or a jagged array.

    using System;
    class ParameterTest
    {
                static int ParameterType(params int[] paramsParameter)
                {
                         int testParameter = 0;
                         foreach(int intParams in paramsParameter)
                         {
                                   testParameter = testParameter + intParams;
                         }
                         return testParameter;
                }
                static void Main()
                {
                          Console.WriteLine(ParameterType(1,2,3));
                          Console.WriteLine(ParameterType1,2,3,4,5));                           
                 }
    }

    Output of the above program would be 6 and 15.

    Note:

    The value passed for a "params" parameter can be either comma separated value list or a single dimensional array. "params" parameters are input only.

    Thanks
    Manoj(InfoAxon Technologies Limited)

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS