---
title: "What is out keyword in C#?"  
description: "What is out keyword in C#?"  
author: "Revati S Misra"  
published: 2024-06-11  
updated: 2024-06-13  
canonical: https://www.mindstick.com/forum/160715/what-is-out-keyword-in-c-sharp  
category: "c#"  
tags: ["c#", "keywords"]  
reading_time: 3 minutes  

---

# What is out keyword in C#?

What is out [keyword in C#](https://www.mindstick.com/forum/253/difference-between-ref-and-out-keyword-in-c-sharp)?

## Replies

### Reply by Ashutosh Patel

### C# out Keyword

In C#, the **out** [keyword](https://www.mindstick.com/forum/33572/sql-inner-join-keyword) is used to pass arguments to methods by reference instead of by value. This allows the method to change the value of the argument and that change is reflected outside the method. It is especially useful when you need a way to return multiple values.

Here are some examples to illustrate the use of the out keyword,

#### Key Points

## Parameter Declaration-

- The `out`keyword must be specified in the method declaration and method calls.
- The parameter passed `out`does not need to be initialized before it is passed to the method, but a value must be provided in the method before it returns.\

## Method Signature-

- The method signature includes the `out` keyword for any parameters to be passed by reference.

## Multiple Return Values-

- `out` parameters are often used to return multiple values ​​from a method.

#### Using `out`to return multiple values

```cs
using System;
// Define a delegate
public delegate void MyDelegate(string message);
public class Program
{
  public static void Main()
   {
       bool IsDivide = DivideNumber(10, 2, out int Result);
       if(IsDivide)
       {
           Console.WriteLine("Result is: "+ Result);
       }
       else
       {
           Console.WriteLine("Division failed: Please enter valid number");
       }
       static bool DivideNumber(int dividend, int divisor, out int result)
       {
           if(divisor ==0)
           {
               result =0;
               return false;
           }
           else
           {
               result = dividend/divisor;
               return true;
           }
       }
   }
}
```

## Output-

```cs
Result is: 5
```

## If enter divisor value 0 (zero)

```cs
Division failed: Please enter valid number
```

#### Using `out` with `TryParse`

```cs
using System;
// Define a delegate
public delegate void MyDelegate(string message);
public class Program
{
  public static void Main()
   {
       Console.WriteLine("Enter string for convert into number");
       string str = Console.ReadLine();

       // Verify the entered value is valid or not
       if(int.TryParse(str, out int Result))
       {
           Console.WriteLine("Number is: "+ Result);
       }
       else
       {
           Console.WriteLine("Conversion failed: enter valid format");
       }
   }
}
```

## Output 1-

```cs
Enter string for convert into number
1524
Number is: 1524
```

## Output 2-

```cs
Enter string for convert into number
1234abc
Conversion failed: enter valid format
```

The out keyword provides a way to return multiple values ​​from a single method, increasing the simplicity and efficiency of your method in C#.

**Also Read:** [How to convert string into int in C#?](https://www.mindstick.com/forum/160716/how-to-convert-string-into-int-in-c-sharp)


---

Original Source: https://www.mindstick.com/forum/160715/what-is-out-keyword-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
