---
title: "What's the difference between the “ref” and “out” keywords in C#?"  
description: "What's the difference between the “ref” and “out” keywords in C#?"  
author: "Steilla Mitchel"  
published: 2023-07-12  
updated: 2023-07-13  
canonical: https://www.mindstick.com/forum/159058/what-s-the-difference-between-the-ref-and-out-keywords-in-c-sharp  
category: "c#"  
tags: ["c#", "keyword research"]  
reading_time: 2 minutes  

---

# What's the difference between the “ref” and “out” keywords in C#?

What's the [difference](https://www.mindstick.com/articles/157114/good-news-or-bad-news-and-the-difference-is) between the “[ref](https://www.mindstick.com/interview/217/what-is-the-difference-between-ref-out-parameters)” and “out” [keywords in C#](https://www.mindstick.com/interview/23293/difference-between-ref-and-out-keywords-in-c-sharp)?

## Replies

### Reply by Aryan Kumar

The `ref` and `out` [keywords](https://www.mindstick.com/articles/12899/how-to-spot-if-you-re-optimizing-for-the-wrong-keywords) in C# are used to pass parameters by reference. However, there are some key differences between the two keywords.

- `ref` **parameters can be initialized before they are passed to the method.** `out` parameters cannot be initialized before they are passed to the method.
- `ref` **parameters can be used to modify the value of the variable that is passed to the method.** `out` parameters can only be used to get the value of the variable that is passed to the method.

In other words, `ref` parameters allow for bi-directional data flow, while `out` parameters only allow for unidirectional data flow.

Here is an example of how the `ref` keyword can be used:

C#

```plaintext
int myNumber = 10;

void DoSomething(ref int number)
{
    number = 20;
}

DoSomething(ref myNumber);

Console.WriteLine(myNumber); // This will print 20
```

In this example, the `DoSomething()` method takes a `ref` parameter called `number`. The `ref` keyword tells the compiler that the `number` parameter is a reference to the `myNumber` variable. This means that any changes that are made to the `number` parameter in the `DoSomething()` method will be reflected in the `myNumber` variable.

Here is an example of how the `out` keyword can be used:

C#

```plaintext
int myNumber = 10;

int AnotherNumber;

void DoSomething(out int number)
{
    number = 20;
}

DoSomething(out AnotherNumber);

Console.WriteLine(AnotherNumber); // This will print 20
```

In this example, the `DoSomething()` method takes an `out` parameter called `number`. The `out` keyword tells the compiler that the `number` parameter is a reference to a new variable that will be created in the `DoSomething()` method. This means that the `number` parameter cannot be initialized before it is passed to the `DoSomething()` method. However, the `number` parameter can be used to get the value of the new variable that is created in the `DoSomething()` method.


---

Original Source: https://www.mindstick.com/forum/159058/what-s-the-difference-between-the-ref-and-out-keywords-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
