I'm making a method to fetch a list of filenames from a server but I have come to a problem that I cannot answer.
The method returns two things:
SftpResult
which is an enum with a variety of return codes.Of these three signatures:
public static ArrayList GetFileList(string directory, out SftpResult result)
public static SftpResult GetFileList(string directory, out ArrayList fileNames)
public static SftpFileListResult GetFileList(string directory)
(where SftpFileListResult
is a composite object of an SftpResult
and an ArrayList
)
which is preferred and why?
Generally, use an output parameter for anything that needs to be returned. When you want to return only one item with only an integer data type then it is better to use a return value. Generally, the return value is only to inform success or failure of the Stored Procedure.
Return parameters are parameters that are available after the planning phase of a workflow. The values returned by these parameters are useful in debugging a workflow. You should understand how return parameters work and what parameters can be used as return parameters to debug workflows.
The out parameter in C# is used to pass arguments to methods by reference. It differs from the ref keyword in that it does not require parameter variables to be initialized before they are passed to a method. The out keyword must be explicitly declared in the method's definition as well as in the calling method.
There is no real difference. Out parameters are in C# to allow method return more then one value, that's all.
Personally I prefer the last option (although using a List<T>
or ReadOnlyCollection<T>
instead of ArrayList
). out
parameters are basically a way of returning multiple values, and it's generally nicer to encapsulate those.
Another option in .NET 4 would be
Tuple<SftpResult, ArrayList> GetFileList(string directory)
That explicitly says, "this method returns two things... I've packed them together for you for this particular case, but it's not worth further encapsulating them: they're not worth composing in a separate type".
(If you're not using .NET 4 you could always write your own Tuple
type.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With