---
title: "What are params, in, out, and ref keywords in C#?"  
description: "What are params, in, out, and ref keywords in C#?"  
author: "Anubhav Sharma"  
published: 2025-06-19  
updated: 2025-06-23  
canonical: https://www.mindstick.com/forum/161738/what-are-params-in-out-and-ref-keywords-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# What are params, in, out, and ref keywords in C#?

What are `params`, `in`, `out`, and `ref` [keywords in C#](https://www.mindstick.com/forum/159058/what-s-the-difference-between-the-ref-and-out-keywords-in-c-sharp) ?

## Replies

### Reply by Ponu Maurya

In C#, the [keywords](https://www.mindstick.com/articles/12899/how-to-spot-if-you-re-optimizing-for-the-wrong-keywords) `params`, `in`, `out`, and `ref` are used to control how arguments are passed to methods. They allow more flexible method signatures, especially when dealing with dynamic argument counts or modifying variables inside a method. Here's a breakdown of each:

## `params` – Variable Number of Arguments

The `params` keyword allows you to pass a **variable number of arguments** of a specified type to a method. It must be the **last parameter** in the method signature, and only one `params` parameter is allowed.

### Example:

```cs
public void PrintNumbers(params int[] numbers)
{
    foreach (int number in numbers)
    {
        Console.WriteLine(number);
    }
}
```

You can call it like:

```plaintext
PrintNumbers(1, 2, 3, 4);
```

**Use Case**: Simplifies calling methods with a list of values without explicitly creating an array.

## `ref` – Pass by Reference (Input/Output)

The `ref` keyword is used when a method **needs to read and modify the caller's variable**. The caller must **initialize** the variable before passing it.

### Example:

```cs
public void DoubleValue(ref int number)
{
    number *= 2;
}

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

**Use Case**: When you want the method to change the original value passed from the caller.

## `out` – Output Parameter (Only for Returning)

The `out` keyword is used to return **multiple values** from a method. Unlike `ref`, the variable **does not need to be initialized** before being passed. However, the method **must assign** a value before returning.

### Example:

```cs
public void GetDetails(out string name, out int age)
{
    name = "Alice";
    age = 30;
}

string name;
int age;
GetDetails(out name, out age);  // name = "Alice", age = 30
```

**Use Case**: Useful when you want to return more than one value from a method, especially before `Tuple` or `ValueTuple` became popular.

## `in` – Pass by Reference (Read-Only)

Introduced in C# 7.2, the `in` keyword passes arguments **by reference**, but they are **read-only** inside the method. It improves performance for large value types by avoiding copies, while still preventing modification.

### Example:

```cs
public void ShowPoint(in Point p)
{
    Console.WriteLine($"X: {p.X}, Y: {p.Y}");
    // p.X = 5; // ❌ Not allowed
}
```

**Use Case**: Best used with large structs to avoid copying but ensure immutability.

## Summary Table

| Keyword | Passed By | Needs Initialization? | Can Modify in Method? | Can Return Value? |
| --- | --- | --- | --- | --- |
| `params` | Value | Optional | No | No |
| `ref` | Reference | Yes | Yes | Yes |
| `out` | Reference | No | Yes (must assign) | Yes |
| `in` | Reference | Yes | No | No |

## When to Use What?

- Use `params` for flexible argument counts.
- Use `ref` to read and modify a variable.
- Use `out` to return multiple results.
- Use `in` for performance on large structs with read-only intent.


---

Original Source: https://www.mindstick.com/forum/161738/what-are-params-in-out-and-ref-keywords-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
