Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Value parameters vs Reference parameters vs Output Parameters in C#
In C#, there are three ways to pass parameters to methods: value parameters, reference parameters, and output parameters. Each mechanism behaves differently in terms of how data is passed and whether changes affect the original variables.
Syntax
Following are the syntax forms for the three parameter types −
// Value parameter (default)
public void Method(int value) { }
// Reference parameter
public void Method(ref int value) { }
// Output parameter
public void Method(out int value) { }
Value Parameters
Value parameters copy the actual value of an argument into the formal parameter of the function. This is the default mechanism for passing parameters. When a method is called, a new storage location is created for each value parameter, and the values are copied into them. Changes made to the parameter inside the method have no effect on the original argument.
Example
using System;
class Program {
public static void ModifyValue(int num) {
num = 100;
Console.WriteLine("Inside method: " + num);
}
public static void Main() {
int originalValue = 50;
Console.WriteLine("Before method call: " + originalValue);
ModifyValue(originalValue);
Console.WriteLine("After method call: " + originalValue);
}
}
The output of the above code is −
Before method call: 50 Inside method: 100 After method call: 50
Reference Parameters
Reference parameters use the ref keyword and pass a reference to the memory location of a variable. Unlike value parameters, no new storage location is created. The reference parameter represents the same memory location as the actual parameter, so changes inside the method affect the original variable.
Example
using System;
class Program {
public static void ModifyReference(ref int num) {
num = 100;
Console.WriteLine("Inside method: " + num);
}
public static void Main() {
int originalValue = 50;
Console.WriteLine("Before method call: " + originalValue);
ModifyReference(ref originalValue);
Console.WriteLine("After method call: " + originalValue);
}
}
The output of the above code is −
Before method call: 50 Inside method: 100 After method call: 100
Output Parameters
Output parameters use the out keyword and are designed to return multiple values from a method. Unlike reference parameters, output parameters transfer data out of the method rather than into it. The variable doesn't need to be initialized before passing, but the method must assign a value to the output parameter.
Example
using System;
class Program {
public static void Calculate(int a, int b, out int sum, out int product) {
sum = a + b;
product = a * b;
}
public static void Main() {
int num1 = 10, num2 = 5;
int result1, result2;
Calculate(num1, num2, out result1, out result2);
Console.WriteLine("Sum: " + result1);
Console.WriteLine("Product: " + result2);
}
}
The output of the above code is −
Sum: 15 Product: 50
Comparison
| Parameter Type | Keyword | Data Flow | Original Variable Affected |
|---|---|---|---|
| Value | None (default) | Into method only | No |
| Reference | ref | Both ways | Yes |
| Output | out | Out of method only | Yes |
Conclusion
Value parameters copy data and don't affect original variables, reference parameters (ref) allow bidirectional data flow and modify originals, while output parameters (out) are designed specifically for returning multiple values from methods. Choose the appropriate type based on whether you need to modify the original variable or return multiple results.
