HomeSearch

C# out Parameter

Use the out keyword on arguments. Out parameters help simplify some methods.

Out.

This C# keyword signifies a reference parameter. Sometimes methods must return more than one value and not store class state.Parameters

Out,

a keyword, fills these requirements. With it we pass parameters whose changes are realized in their calling methods. In a method, an out argument must be assigned.

An example.

The .NET Framework provides robust support for creating object-oriented logic. But it also provides a way to return many values from a method without using objects.Return

First: In Main, three local bool variables are declared. These 3 variables are not initialized.

Bool

Then: Those same bool variable locations on the stack are initialized to the false Boolean literal in the TestString method called next.

And: In the loop in TestString, if certain characters occur in the string, the bool parameters are assigned true.

True, False

Info: TestString() can be used in programs to find multiple properties of the string in a single pass through the string's characters.

C# program that uses out Boolean parameters using System; class Program { static void Main() { bool period; // Used as out parameter. bool comma; bool semicolon; const string value = "has period, comma."; // Used as input string. TestString(value, out period, out comma, out semicolon); Console.WriteLine(value); // Display value. Console.Write("period: "); // Display labels and bools. Console.WriteLine(period); Console.Write("comma: "); Console.WriteLine(comma); Console.Write("semicolon: "); Console.WriteLine(semicolon); } static void TestString(string value, out bool period, out bool comma, out bool semicolon) { // Assign all out parameters to false. period = comma = semicolon = false; for (int i = 0; i < value.Length; i++) { switch (value[i]) { case '.': { period = true; // Set out parameter. break; } case ',': { comma = true; // Set out parameter. break; } case ';': { semicolon = true; // Set out parameter. break; } } } } } Output has period, comma. period: True comma: True semicolon: False

A discussion.

The out descriptor tells the intermediate language compiler to copy the actual variable storage location, not just the value at a storage location.

Tip: Out parameters are often most useful for value types such as bool, int or short.

Tip 2: There is no other easy way to return multiple values without allocating an array or object.

Multiple Return Values

Inline out parameters.

We can combine statements when calling a method that requires an out parameter. We can declare the variable directly inline with the method call.

Warning: This syntax will not work with older versions of the .NET Framework. So be sure to be targeting .NET 4.7 or later.

C# program that uses inline out parameter syntax using System; class Program { static bool Test(out int size) { // This method has an out parameter. size = 1000; return size >= 1000; } static void Main() { // Declare out parameter directly inside method call. if (Test(out int size)) { Console.WriteLine(size); } } } Output 1000

Definite assignment.

The C# compiler applies definite assignment analysis. The out keyword ensures that variables are always assigned in the body of a method.RefDefinite Assignment

With out,

a method can signal to the calling location that the variable location's were initialized and written. Out simplifies code where multiple function calls can be combined.
Home
Dot Net Perls
© 2007-2019 Sam Allen. All rights reserved. Written by Sam Allen, info@dotnetperls.com.