Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

out: practical reference

out: practical reference: out passes a parameter by reference; the callee must definitely assign it before a normal return and may read it after that assignment.

Version: C# language; verified against C# 14 reference

Syntax

out /* grammar-specific form */

out passes a parameter by reference; the callee must definitely assign it before a normal return and may read it after that assignment.

Use it for a Try-pattern that returns bool plus an additional value.

Many out parameters reduce readability; consider a tuple or result type.

Even when returning false, the method must assign out, commonly to default.

The complete program actually uses out; its exact expected stdout is True\n7.

Examples

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;

Console.WriteLine(int.TryParse("7", out var value)); Console.WriteLine(value);

Result

True
7

The complete program actually uses `out`; its exact expected stdout is `True\n7`.

Sources