What is the difference between Ref and Out keywords in C#?

We use ref and out keywords to pass parameters to a method. What exactly the different between these 2 keywords?

Here is an example:

// A simple example for usage of Ref keyword in C#

static void Main(string[] args)
{
    int myVariable = 5;

    string returnText = PrintNumber(ref myVariable);

    Console.WriteLine(returnText);
}

public static string PrintNumber(ref int myVariable)
{
    string returnText = "Result: " + myVariable.ToString();

    return returnText;
}

Output:

You may ask: “Why do we need to use ref or out? Can’t we just pass the parameter without using them?. The answer is “Yes”. However, if you don’t use ref or out, the parameters are sent to methods as values by default. It means, the parameter you pass becomes a new variable in the called method. If you change its value, the variable in the main method won’t change.

In contrast, if you use ref or out keywords,  parameter is passed to the method as reference. It means, it is still the same variable in the main method and called method. If you change the value of this variable, the value of the variable in the main method will change too.

Here is the extended version of our example above:

// Changing the value of the parameter passed by Ref keyword in C#
static void Main(string[] args)
{
    int myVariable = 5;

    Console.WriteLine("Initial value (in the main method): " + myVariable.ToString());

    string returnText = PrintNumber(ref myVariable);

    Console.WriteLine("New value (in the main method): " + returnText.ToString());
}

public static string PrintNumber(ref int myVariable)
{
    Console.WriteLine("Initial value (in the called method): " + myVariable.ToString());

    myVariable++;

    Console.WriteLine("New value (in the called method): " + myVariable.ToString());

    return myVariable.ToString();
}

Output:

Difference between Ref and Out keywords

The main difference is that you have to initialize (or assign a value to) variable in the main method before passing it by ref keyword. However, you don’t have to initialize it in the main method if you are passing it by out parameter.

// The example that shows the difference between ref and out keywords in C#

static void Main(string[] args)
{
    int myVariable = 5;

    string returnText1 = PrintNumber1(ref myVariable);
    string returnText2 = PrintNumber2(out myVariable);

    Console.WriteLine("Passed by ref: " + returnText1.ToString());
    Console.WriteLine("Passed by out: " + returnText2.ToString());

    Console.ReadLine();
}

public static string PrintNumber1(ref int myVariable)
{
    // No initialization here

    return myVariable.ToString();
}

public static string PrintNumber2(out int myVariable)
{
    myVariable = 6; // It must be initialized here

    return myVariable.ToString();
}

Output:

In the called method, if you don’t initialize a parameter passed by out keyword, you will receive the error messages below:

The out parameter ‘myVariable’ must be assigned to before control leaves the current
Use of unassigned out parameter ‘myVariable’

Comparison table

Ref Out
The parameter must be initialized before it is passed a method Parameter doesn’t have to be initialized in the main method
It is not required to initialize (or assign a value to) the parameter in the called method Parameter has to be initialized or a value should be assigned to it in the called method
Ref is useful when the called method needs to modify the passed parameter Out is useful when multiple values need to be returned from a function or method
Data can be passed bi-directionally. It work two-ways Data is passed only one-way (from the called method to the main method)

Notes:

  • Both ref and out keywords are treated the same at compile time but they are treated differently at run time
  • Properties cannot be passed by ref or out keywords

Bonus 1:

Starting from C# 7.0, you can declare variables on the fly while calling a method with out parameter:

int.TryParse("123", out int x);
Console.WriteLine(x);

The code lines above are equivalent to the following lines:

int x;
int.TryParse("123", out x);
Console.WriteLine(x);

Bonus 2:

You don’t have to pass variables/values for every out parameter defined by a method. You can ignore out parameters by simply passing an underscore (_). For example:

TestMethod(out _, out _, out int number, out _)

In the example above, we ignored all out parameters except the third one.

References

How to enable Assembly Binding Logging debugging .NET applications?

If you receive an “Assembly not found” error for your .NET application hosted in IIS, you may feel hopeless for not having much debugging details. IIS doesn’t show the binding trace as this slows down applications. However, there is a way to enable logging.

Example of an assembly/DLL binding issue:

Could not load file or assembly ‘Oracle.DataAccess’ or one of its dependencies. An attempt was made to load a program with an incorrect format.

Solution

Use this quick trick to enable Assembly Binding Logging so that you can see where the binding is failing.

  1. Go to “Start > Run”
  2. Enter “regedit
  3. Go to this key: “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Fusion”
  4. Change the value of “EnableLog” to “1”. If this key doesn’t exist, create it with DWORD(32-bit) type

Note: Another way of viewing details for failed assembly bindings is to use Microsoft’s Fuslogvw tool. This tool is installed with Visual Studio. Refer to the link below for more details.

Reference:

Solved: “Unknown server tag telerik:RadScriptManager”

If you are using third-party .NET component libraries such as Telerik or DevExpress, you may come across the error message below while trying to include controls in your page.

Server Error in ‘/TestASPNET’ Application.

Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: Unknown server tag ‘telerik:RadScriptManager’.

Solution

Make sure to include 2 DLL files below in your project references of Visual Studio:

  • Telerik.Web.UI
  • Telerik.Web.UI.Skins

If you are still receiving the error above, add a reference to your Telerik assembly in your web.config file. You can use the following lines to make this reference.

<system.web>
  <pages viewStateEncryptionMode="Always" controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID">
    <controls>
      <add tagPrefix="telerik" namespace="Telerik.Web.UI" assembly="Telerik.Web.UI"/>
    </controls>
  </pages>
</system.web>