Programming

What are ref and out parameters in C#?

C
Chakrapani Upadhyaya
Author & Content Creator
December 09, 2025 15 min read 23 views

Why are they used?

They’re used when you want a method to be able to update or return more than one value by changing the input variables directly. They help with situations where you need functions returning multiple results, or when you want efficient variable updates.

Differences

Aspectref Parameterout Parameter
InitializationMust be initialized BEFORE passing to the methodDoesn’t need to be initialized before passing to method
Requirement in MethodCan be read and written insideMust be assigned (written) inside the method before exiting
PurposePassing values to method and getting updated value backOnly for output — method returns values using variable

Real World Scenario

Suppose you are building a method that parses a string and returns a result, but also needs to tell you whether the parse was successful. C#’s built-in int.TryParse uses the out keyword:


C#
int number;
bool success = int.TryParse("123", out number);

Here, number is only used for output. You don’t need to initialize it before calling TryParse. The function assigns its value inside.

In contrast, imagine a method that takes a value, updates it, and also reads its current state:

C#
void Increment(ref int x) {
    x += 1;
}
int value = 5;
Increment(ref value); // value is now 6

With ref, the variable must be initialized first, because the method may use both its current and updated values.


Pros and Cons

ref

  • Pros:
    • Allows both input and output.
    • Can read and write the passed variable.
  • Cons:
    • Variable must be initialized before the method call.
    • May make code harder to read (unpredictable updates).

out

  • Pros:
    • Useful for returning multiple outputs from a method.
    • No need to initialize before calling.
  • Cons:
    • Can’t be used for input, only for output.
    • Must assign inside method — otherwise, error.

Example with Explanation

C#
// Using ref
void DoubleValue(ref int a) {
    a = a * 2;
}

// Using out
void GetSumAndProduct(int x, int y, out int sum, out int product) {
    sum = x + y;
    product = x * y;
}

int val = 10;
DoubleValue(ref val); // val is now 20

int s, p;
GetSumAndProduct(3, 4, out s, out p); // s=7, p=12
  • DoubleValue needs val to be initialized before passing.
  • GetSumAndProduct doesn’t need s or p initialized; the method sets both.

Conclusion

Use ref when you need a method to access the variable’s current value and potentially update it. Use out when you just want the method to provide a new value for a variable, often when returning multiple results from a function. Always remember:

  • ref = read and write, needs initialization.
  • out = write only, no initialization needed.

Choosing the right one makes your code clear and fits your specific needs when passing data in and out of methods.

Share:
C

About Chakrapani Upadhyaya

Senior Full stack Developer

Subscribe!

Get the best travel tips and exclusive content delivered to your inbox

Best travel tips
Monthly newsletter
No spam ever