Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional Output Parameters [duplicate]

Tags:

c#

c#-4.0

In C# 4, is there a good way to have an optional output parameter?

like image 409
Jeremy Mattingly Avatar asked Nov 27 '10 20:11

Jeremy Mattingly


People also ask

Can an out parameter be optional?

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.

What is the best way of declaring an out parameter that is never going to be used?

While you can't actually make the out parameter optional, you could simply create an overload for the function without the out parameter, which would then take away the need to create a temporary variable. This answer is more functional. No need to modify later on if you want to use it or not use it anymore.

Why are the optional parameters are added?

Optional Parameters are parameters that can be specified, but are not required. This allows for functions that are more customizable, without requiring parameters that many users will not need.


1 Answers

Not really, though you can always overload the method with another one that does not take the output parameter.

like image 70
Cameron Avatar answered Oct 13 '22 06:10

Cameron