Home
C#
out Keyword (out Parameter)
This page was last reviewed on May 29, 2023.
Dot Net Perls
Out. This C# keyword signifies a reference parameter. Sometimes methods must return more than one value and not store class state.
Parameter
Keyword info. Out 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 C# language has robust support for object-oriented logic. But it also provides a way to return many values from a method without using objects.
return
Part 1 In Main, 3 local bool variables are declared—these 3 variables are not initialized.
bool
Part 2 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.
using System; class Program { static void Main() { // Part 1: variables used as out parameter. bool period; bool comma; bool semicolon; const string value = "has period, comma."; TestString(value, out period, out comma, out semicolon); Console.WriteLine(value); Console.Write("period: "); 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) { // Part 2: 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; } } } } }
has period, comma. period: True comma: True semicolon: False
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.
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); } } }
1000
Discard name syntax. Sometimes we need to call a method with "out" parameters, but do not need to use those parameters. We can avoid using a "dummy" name for those locals.
Detail The underscore char is a "discard name." The parameter exists, but is not referenced, and does not need a name.
using System; class Program { static void Test(out int size, out string name) { size = 10; name = "bird"; } static void Main() { // We do not need the out int, so use an underscore name (discard name). Test(out _, out string name); Console.WriteLine("NAME: {0}", name); } }
NAME: bird
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
Definite assignment. The C# compiler applies definite assignment analysis. The out keyword ensures that variables are always assigned in the body of a method.
ref
A summary. With out, a method can signal to the calling location that the variable location's were initialized and written. Out simplifies code that returns values.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on May 29, 2023 (simplify).
Home
Changes
© 2007-2024 Sam Allen.