ref and out are parameter modifiers in C# that allow a method to pass values by reference. The main difference is that a ref parameter must be initialized before it is passed, while an out parameter does not need to be initialized before being passed.
- ref is used when a method needs to read and modify an existing value.
- out is used when a method needs to return a value through a parameter.
Example: Program to demonstrate how ref modifies an existing variable, while out assigns a value to a variable through a method parameter.
using System;
class GFG {
// Main method
static public void Main()
{
// Declaring variable
// without assigning value
int G;
// Pass variable G to the method
// using out keyword
Sum(out G);
// Display the value G
Console.WriteLine("The sum of" +
" the value is: {0}", G);
}
// Method in which out parameter is passed
// and this method returns the value of
// the passed parameter
public static void Sum(out int G)
{
G = 80;
G += G;
}
}
Output
The sum of the value is: 160
Explanation: The ref parameter a must be initialized before calling the method, while the out parameter b does not need to be initialized. Both methods modify the original variables.
ref Keyword
The ref keyword is used to pass a parameter by reference. It allows the method to work directly with the original variable, so changes made inside the method affect the original value.
- The variable must be initialized before passing it with ref.
- The ref keyword must be used in both the method declaration and method call.
Example: The ref parameter changes the original variable from 50 to 100.
using System;
class GFG
{
static void ChangeValue(ref int x)
{
x = 100;
}
static void Main()
{
int num = 50;
ChangeValue(ref num);
Console.WriteLine(num);
}
}
Output
100
out Keyword
The out keyword is used to pass a parameter by reference when a method needs to return a value through that parameter. The variable does not need to be initialized before it is passed.
- The variable does not need to be initialized before passing it with out.
- The method must assign a value to the out parameter before it returns.
Example: The out parameter assigns the value 100 to num.
using System;
class GFG
{
static void GetValue(out int x)
{
x = 100;
}
static void Main()
{
int num;
GetValue(out num);
Console.WriteLine(num);
}
}
Output
100
Difference between Ref and Out keywords
Feature | ref Keyword | out Keyword |
|---|---|---|
Purpose | Allows a method to access and modify an existing variable. | Allows a method to assign a value to a variable. |
Initialization | Variable must be initialized before passing it. | Variable does not need to be initialized before passing it. |
Assignment Inside Method | Assigning a new value is optional. | Method must assign a value before returning normally. |
Accessing Value | Method can read the existing value immediately. | Method cannot read the parameter before assigning it. |
Value Modification | Changes made inside the method affect the original variable. | Value assigned inside the method is stored in the original variable. |
Common Use Case | Modifying an existing value. | Returning additional values from a method. |
Example | Update(ref num) | GetValue(out num) |