Skip to main content

How to Declare an out Variable Inline in C#

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Some methods hand back a second result through an out argument, such as int.TryParse, which returns whether the text was a number and puts the number itself in an out variable. In C#, you can declare that variable right inside the call: out int number.

Syntax​

Syntax
if (<Method>(<arguments>, out <Type> <name>))
{
<code that uses name>
}
PartWhat it does
outMarks the argument the method writes its extra result into.
<Type> <name>Declares the variable in the call itself. var works too: out var <name>.
if (...)Methods named Try... return true when they succeeded, so the variable is only used when it holds a real result.

Microsoft's method parameters reference: "You can also declare the out variable in the argument list of the method call, rather than in a separate variable declaration."

The Example​

The example parses two pieces of text with int.TryParse: "42", which is a number, and "abc", which isn't. It adds 1 to the first and shows what the variable holds after the second fails.

Prerequisites​

  • The .NET 10 SDK or newer, which runs a single .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs in an empty folder:

main.cs, which prints number plus 1 when int.TryParse of 42 succeeds with out int number, and prints Not a number with the value of other when int.TryParse of abc fails with out int other.

main.cs
if (int.TryParse("42", out int number))
{
Console.WriteLine(number + 1);
}

if (!int.TryParse("abc", out int other))
{
Console.WriteLine($"Not a number. other is {other}.");
}

! turns false into true, so the second block runs when parsing fails.

Step 2: Run It​

Terminal
dotnet run main.cs
Output
43
Not a number. other is 0.

A zsh terminal where dotnet run main.cs prints 43 and Not a number. other is 0.

"42" parsed, so number was 42 and the program printed 43. "abc" didn't, and other was 0. The Int32.TryParse reference says the out value is the parsed number "if the conversion succeeded, or zero if the conversion failed". A 0 after a failure is a placeholder, not a parsed value. The command is the same in zsh, bash and PowerShell.

Why TryParse Instead of Parse​

int.Parse returns the number directly, and throws when the text isn't one:

main.cs
Console.WriteLine(int.Parse("abc"));
Output
Unhandled exception. System.FormatException: The input string 'abc' was not in a correct format.
at System.Number.ThrowFormatException[TChar](ReadOnlySpan`1 value)
at System.Int32.Parse(String s)
at Program.<Main>$(String[] args) in /Users/you/demo/main.cs:line 1

For text a person typed or a file supplied, TryParse with an inline out variable is the safer choice. Its reference: "The TryParse method is like the Parse method, except the TryParse method does not throw an exception if the conversion fails."

Common Mistakes​

MistakeWhat happensDo this instead
Using the out value without checking the return valueA failed parse gives 0, which looks like a real numberUse the variable only inside if (int.TryParse(...))
Calling int.Parse on text that may not be a numberFormatException: The input string 'abc' was not in a correct format.Use int.TryParse
Declaring the variable on a separate line firstWorks, but takes an extra line, and the variable exists before it means anythingDeclare it inline: out int number

Troubleshooting​

SymptomLikely causeFix
A value is 0 although the text looked like a numberParsing failed, for example because of a letter or a decimal point, such as "4.2"Check the return value of TryParse
FormatExceptionParse received text that isn't a numberSwitch to TryParse

FAQ​

Can I write out var instead of the type?​

Yes. The reference: "You can also declare an implicitly typed local variable." int.TryParse("42", out var number) gives number the type int.

What if I only need to know whether it parsed?​

Use the discard _ instead of a name: int.TryParse(text, out _) returns true or false and throws the number away.

Conclusion​

if (int.TryParse(text, out int number)) declares the out variable inside the call and uses it only when parsing worked. After a failure, the variable is 0, so always check the return value first.

Additional Resources​