---
title: "What is ref key word in C#?"  
description: "What is ref key word in C#?"  
author: "Ravi Vishwakarma"  
published: 2024-06-11  
updated: 2024-06-11  
canonical: https://www.mindstick.com/interview/33904/what-is-ref-key-word-in-c-sharp  
category: "c#"  
tags: ["c#", ".net"]  
reading_time: 2 minutes  

---

# What is ref key word in C#?

The `ref` keyword is used to **pass** arguments to[**methods by reference**](https://www.mindstick.com/forum/159208/what-is-java-pass-by-reference-or-pass-by-value), rather than by **value**. This means that any changes made to the parameter within the method will affect the original variable outside of the method.

Here's how it works:

```cs
using System;

class Program
{
    static void Main()
    {
        int num = 10;
        Console.WriteLine("Before calling the method: " + num);
        // Passing 'num' variable by reference
        ChangeNumber(ref num);
        Console.WriteLine("After calling the method: " + num);
    }

    static void ChangeNumber(ref int x)
    {
        x = 20; // Changes the value of 'num' in Main method
    }
}
```

## Output:

```cs
Before calling the method: 10
After calling the method: 20
```

Without the `ref` keyword, changes made to the parameter `x` inside the `ChangeNumber` the method would not affect the original `num` variable.

## Read more -

[**Difference between static, readonly, and constant in C#**](https://www.mindstick.com/interview/33896/difference-between-static-readonly-and-constant-in-c-sharp)

[**What is the use of "p" tag in HTML**](https://www.mindstick.com/interview/33888/what-is-the-use-of-p-tag-in-html)

[**How do you change the fonts of the webpage?**](https://www.mindstick.com/interview/33886/how-do-you-change-the-fonts-of-the-webpage)

## Answers

### Answer by Ravi Vishwakarma

The `ref` keyword is used to **pass** arguments to[**methods by reference**](https://www.mindstick.com/forum/159208/what-is-java-pass-by-reference-or-pass-by-value), rather than by **value**. This means that any changes made to the parameter within the method will affect the original variable outside of the method.

Here's how it works:

```cs
using System;

class Program
{
    static void Main()
    {
        int num = 10;
        Console.WriteLine("Before calling the method: " + num);
        // Passing 'num' variable by reference
        ChangeNumber(ref num);
        Console.WriteLine("After calling the method: " + num);
    }

    static void ChangeNumber(ref int x)
    {
        x = 20; // Changes the value of 'num' in Main method
    }
}
```

## Output:

```cs
Before calling the method: 10
After calling the method: 20
```

Without the `ref` keyword, changes made to the parameter `x` inside the `ChangeNumber` the method would not affect the original `num` variable.

## Read more -

[**Difference between static, readonly, and constant in C#**](https://www.mindstick.com/interview/33896/difference-between-static-readonly-and-constant-in-c-sharp)

[**What is the use of "p" tag in HTML**](https://www.mindstick.com/interview/33888/what-is-the-use-of-p-tag-in-html)

[**How do you change the fonts of the webpage?**](https://www.mindstick.com/interview/33886/how-do-you-change-the-fonts-of-the-webpage)


---

Original Source: https://www.mindstick.com/interview/33904/what-is-ref-key-word-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
