Introduction
In C#, when you pass a parameter to a method, it can be done in two ways: call by value and call by reference. Call by reference allows a method to modify the actual value of the variable being passed. When a parameter is passed by reference, the method receives a reference to the original variable, not a copy of its value. Any changes made to the parameter inside the method affect the original value.
How Call By Reference Works
When you pass parameters by reference:
- The called method receives a reference to the original variable.
- Changes made to the parameter inside the method affect the original variable.
Syntax
Using ref
Keyword
void MethodName(ref dataType parameter)
{
// Code to execute
}
Using out
Keyword
void MethodName(out dataType parameter)
{
// Code to execute
}
Example: Using ref
Keyword
Here’s a complete example demonstrating call by reference using the ref
keyword in C#:
using System;
namespace CallByReferenceExample
{
class Program
{
static void Main(string[] args)
{
int number = 10;
Console.WriteLine("Before calling ChangeValue: " + number);
// Call ChangeValue method with number as argument
ChangeValue(ref number);
Console.WriteLine("After calling ChangeValue: " + number);
}
// Method that changes the value of the parameter
static void ChangeValue(ref int value)
{
value = 20; // This change will affect the original variable
Console.WriteLine("Inside ChangeValue: " + value);
}
}
}
Output
Before calling ChangeValue: 10
Inside ChangeValue: 20
After calling ChangeValue: 20
Explanation
- Original Value:
int number = 10;
initializes thenumber
variable to 10. - Method Call:
ChangeValue(ref number);
calls theChangeValue
method withnumber
as the argument, passed by reference using theref
keyword. - Inside Method:
value = 20;
changes the value of the parametervalue
inside the method. - Output: The change inside the method affects the original variable
number
.
Example: Using out
Keyword
The out
keyword is used to pass parameters by reference, but it requires the method to assign a value to the parameter before the method exits.
using System;
namespace CallByReferenceOutExample
{
class Program
{
static void Main(string[] args)
{
int number;
InitializeValue(out number);
Console.WriteLine("After calling InitializeValue: " + number);
}
// Method that initializes the value of the parameter
static void InitializeValue(out int value)
{
value = 30; // Assign a value to the parameter
Console.WriteLine("Inside InitializeValue: " + value);
}
}
}
Output
Inside InitializeValue: 30
After calling InitializeValue: 30
Explanation
- Original Declaration:
int number;
declares thenumber
variable without initializing it. - Method Call:
InitializeValue(out number);
calls theInitializeValue
method withnumber
as the argument, passed by reference using theout
keyword. - Inside Method:
value = 30;
assigns a value to the parametervalue
inside the method. - Output: The assigned value inside the method affects the original variable
number
.
Example: Modifying Array Elements
Since arrays are reference types, you can modify their elements even when passed by value. However, passing them by reference allows you to change the array itself:
using System;
namespace ModifyArrayExample
{
class Program
{
static void Main(string[] args)
{
int[] numbers = { 1, 2, 3 };
Console.WriteLine("Before calling ModifyArray: " + string.Join(", ", numbers));
// Call ModifyArray method with numbers as argument
ModifyArray(ref numbers);
Console.WriteLine("After calling ModifyArray: " + string.Join(", ", numbers));
}
// Method that changes the elements of the array
static void ModifyArray(ref int[] values)
{
values = new int[] { 4, 5, 6 }; // Assign a new array to the parameter
Console.WriteLine("Inside ModifyArray: " + string.Join(", ", values));
}
}
}
Output
Before calling ModifyArray: 1, 2, 3
Inside ModifyArray: 4, 5, 6
After calling ModifyArray: 4, 5, 6
Explanation
- Original Array:
int[] numbers = { 1, 2, 3 };
initializes thenumbers
array. - Method Call:
ModifyArray(ref numbers);
calls theModifyArray
method withnumbers
as the argument, passed by reference using theref
keyword. - Inside Method:
values = new int[] { 4, 5, 6 };
assigns a new array to the parametervalues
inside the method. - Output: The assigned value inside the method affects the original array
numbers
.
Conclusion
Call by reference in C# allows a method to modify the original value of a variable by passing a reference to the variable instead of a copy. This can be done using the ref
or out
keywords. Understanding call by reference is important for scenarios where you need to modify the original data or initialize variables within a method. By mastering this concept, you can write more efficient and flexible code in your C# programs.