In C# 4, is there a good way to have an optional output parameter?
-
Don't see any value for such a feature. If you have ever listen to C# compiler guys' sessions on PDC, you will see every changes has a meaning. – Lex Li Nov 28 '10 at 09:57
-
58I disagree that there's no value in this feature. If a method has some logic in it BESIDE returning data via out-parameters - I want to be able to call this logic, just don't need the output... – Alex from Jitbit Sep 03 '11 at 15:19
-
4I use Int32.TryParse() to see if the given string can be parsed into a number and, if it can, sometimes that's all I care about. So Jitbit is right. – Sep 01 '15 at 02:14
-
1It's can be handy to optionally capture sidechain information from a function. Plus, if VB has it, why doesn't C#? It should automatically overload. – Brain2000 May 10 '17 at 18:41
5 Answers
Not really, though you can always overload the method with another one that does not take the output parameter.

- 96,106
- 25
- 196
- 225
-
5any idea how to handle the optional parameter more elegantly than `{ string dummy; return Foo(out dummy); }` in the overload that does not take the output parameter? – Louis Rhys Jul 17 '12 at 03:13
-
3
-
2
-
16In C# 7.0 you can do `Foo(out string dummy);` or even better `Foo(out _);` (underscore) – Alex from Jitbit May 21 '18 at 20:38
-
3the fact that this is possible, shows that it should have been optional in the language design. this seems like an okay solution. although, it does not allow me to put the output parameter after other optional parameters, which can cause typos if i expect the overloads to have the same parameter order – symbiont Dec 16 '19 at 10:09
No.
To make it "optional", in the sense that you don't need to assign a value in the method, you can use ref
.

- 26,990
- 7
- 84
- 140
-
13A `ref` parameter is a very different use case. An optional parameter passes in a specific value if the caller does not specify one -- with a ref parameter, the caller is always specifying a value, whether they feel like they have specified it or not. The `ref` always points to valid memory location, and a valid memory location always contains _something_. At best, this is just a magic value without the benefit of a default specification saying "the default value is X." – danwyand Nov 24 '13 at 23:11
-
4Yea, this answer isn't a design judgment, which is why I added the "in the sense that" qualifier. – codekaizen Oct 16 '14 at 21:58
Decorating the parameters with OptionalAttribute doesn't work, either. To extend the previous sample, you'd get something like:
private void Func(
[Optional] out int optional1,
[Optional] out string optional2)
{ /* ... */ }
Note that the above will compile (perhaps unfortunately). However, attempting to compile:
Func(out i);
will fail unless an overload with a one-parameter signature is explicitly provided.
To (theoretically) make the above work is a significant problem. When a method with an omitted optional parameter is called, a stack frame containing the values of all the parameters is created, and the missing value(s) are simply filled with the specified default values.
However, an "out" parameter is a reference, not a value. If that parameter is optional and not supplied, what variable would it reference? The compiler still needs to enforce the requirement that the "out" parameter be filled before any normal return from the method, as the compiler doesn't know a priori which (if any) optional parameters are specified by a caller. This means that a reference to dummy variable somewhere would have to be passed so the method has something to fill. Managing this dummy variable space would create a nasty headache for the writer of the compiler. I'm not saying that it would be impossible to work out the details of how this would work, but the architectural implications are great enough that Microsoft understandably passed on this feature.

- 91
- 1
- 1
private object[] Func();
assign as many as optional outputs you want in return object array and then use them! but if you mean optional output something like
private void Func(out int optional1, out string optional2)
and then you call something like
Func(out i);
then the answer is no you cant.
also C# and .NET framework hast many many data structures that are very flexible like List
and Array
and you can use them as an output parameter or as return type so there is no need to implement a way to have optional output parameters.