Different between Ref and Out Parameter in C#

Different between Ref and Out Parameter in C#
Description:-

>Both Ref and Out parameters are used to pass  argument within a Method. 
>Ref and Out parameters are useful when the method returns multiple value.
>Params parameter is used to pass a unlimited arguments to a method  => using as single dimensional    array.


Differences:-


There is no diffrence between out and ref , in term of both are pass by reference

Ref  Parameter:- If  want to pass a variable as ref parameter in method , Initialize require before                                       passed to method.
                              >Ref is used to pass  value type argument as a reference
                            ex:-            int i=2; // variable  initialize  to be need.
                                               Sample(ref i);

                             If ref  keyword pass parameter  as a reference ,that means if value of parameter                                    changed then  it will reflect to method also.


Out Parameter:- In this parameter not required  initialization ,only required   before it return value                                   to calling method.
                             >Out is use to return the multiple result value from a method.
                             >A method can have any number of out parameters.
                             ex:-            int i , j; // no need to variable  initialize  .
                                               Sample(out i, out j);
                             




Ref keyword Program:-

using System;
using System.Text;

namespace Test_Application
 {
     class Program
     {

        public  static void sample( ref int x)
        {
            x += 10;
        }
         static void Main(string[] args)
         {
            int i = 2;
            sample(ref i);
            Console.WriteLine(i);
            Console.ReadLine();

         }
     }
 }

Output:-  12


using System;
namespace Tests1014
{
   public class Program
    {
     
        public void Variable()
        {
            int b = 19;                 // it need to intialize variable out side the function.
            Add(ref b);
            Console.WriteLine( b);
            Console.ReadLine();
        }
        public void Add(ref int a)
        {         
            a = a + 10;            <= appropriate execution (modified).
                                          //In care ref value need not need to initialize the variable.
        }
        static void Main(string[] args)
        {
            Program obj = new Program();
            obj.Variable();
        }
    }

}

Output :- 29
without ref key word :- 19
ref allows to pass the values from function to function after appropriate execution.


Out Parameter Keyword Program

using System;
using System.Text;

namespace Test_Application
 {
     class Program
     {
     
        public  static int sample(out int x, out int y)
        {
            x = 8;
            y = 6;
            return 0;
        }
         static void Main(string[] args)
         {
            int i , j;
            sample( out i, out j);
            Console.Write(i);
            Console.Write(" "+j);
            Console.ReadLine();

         }
     }
 }

Output:- 8 6

using System;
namespace Tests1014
{
   public class Program
    {
       
        public void Variable()
        {
            int b = 19;                                    // 19 will won't accept by Out parameter
            Add( out b); 
            Console.WriteLine( b);                //output  will 10
            Console.ReadLine();
        }
        public void Add( out int a)
        {   
            a=0;                            variable initialize inside the function.
            a = a + 10;           
        }
        static void Main(string[] args)
        {
            Program obj = new Program();
            obj.Variable();
        }
    }

}

Output :-  10

In out parameter the it won't accept value from out side



Share this

Previous
Next Post »