Saturday 15 September 2018

What is the ref and Out keyword in C#?

consider the following program to understand the out and ref keyword of C#

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace simplevalue
{
    class Progra {
      static void Addvalue(int val)
        {
             val++
        }
      static
 void Main(string[] args)
       {
            int
 value = 10;
            Addvalue(value);
            Console.WriteLine(value);
            Console.Read();
        }
    }
}


in the above program the output of the above program is 10. because we provide the value variable to 10 after that we provide this value to Addvalue() function and in that they increment the value by 1 and become 11. but this value is never carried out to this function because we copy the value variable not reference to the variable. this is default working of the C# language .
we preferred this type of output in most of the scenario but in some cases where we want to modify the variable then we use ref and out keyword for this.

The out and ref keyword are looks quite similar in nature. Both parameters are used to return back some value to the caller of the function. But still there is a small but important difference between them. Both of the parameter type has been kept in the C# language for specific scenario.

Out keyword:
The out parameter can be used to return the values in the same variable  as a parameter of the method. Any changes made to the parameter will be reflected in the variable.
The out keyword causes arguments to be ed by reference. This is similar to the ref keyword, except that ref requires that the variable be initialized before being ed. To use an out parameter, both the method definition and the calling method must explicitly use the out keyword. 

example:
 static void Main(string[] args)
        {
            int value;
            AddValue( out value);
            Console.WriteLine(value);

            Console.Read();
        }
        static void AddValue(out int val)
        {
            val = 20; 
        }

ref keyword: 
The ref keyword on a method parameter causes a method to refer to the same variable that was ed as an input parameter for the same method. If you do any changes to the variable, they will be reflected in the variable.
The ref keyword is similer to out keyword but ref requires that the variable be initialized before being ed.
 
For example
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace outexample
{
    class Program    {
        static void value(ref int val)
        {
           val = 20;
        }
    static
 void Main(string[] args)
        {
            int value;
            value(ref value);
            Console.WriteLine(value);     
           Console.Read();
        }
    }
}

No comments:

Post a Comment

AdSense

The Ultimate Guide to Interceptors: Understanding Their Power and Functionality

  The Ultimate Guide to Interceptors: Understanding Their Power and Functionality An interceptor is a service that can intercept HTTP reques...

Follow