Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Syntactic sugar for out parameters?

Let us say for a moment that C# allowed multiple return values in the most pure sense, where we would expect to see something like:

string sender = message.GetSender();
string receiver = message.GetReceiver();

compacted to:

string sender, receiver = message.GetParticipants();

In that case, I do not have to understand the return values of the method until I actually make the method call. Perhaps I rely on Intellisense to tell me what return value(s) I'm dealing with, or perhaps I'm searching for a method that returns what I want from a class I am unfamiliar with.

Similarly, we have something like this, currently, in C#:

string receiver;
string sender = message.GetParticipants(out receiver);

where the argument to GetParticipants is an out string parameter. However, this is a bit different than the above because it means I have to preempt with, or at least go back and write, code that creates a variable to hold the result of the out parameter. This is a little counterintuitive.

My question is, is there any syntactic sugar in current C#, that allows a developer to make this declaration in the same line as the method call? I think it would make development a (tiny) bit more fluid, and also make the code more readable if I were doing something like:

string sender = message.GetParicipants(out string receiver);

to show that receiver was being declared and assigned on the spot.

like image 507
JoshJordan Avatar asked Apr 14 '09 16:04

JoshJordan


7 Answers

No, there isn't currently any syntactic sugar around this. I haven't heard of any intention to introduce any either.

I can't say I use out parameters often enough for it really to be a significant concern for me (there are other features I'd rather the C# team spent their time on) but I agree it's a bit annoying.

like image 159
Jon Skeet Avatar answered Oct 02 '22 21:10

Jon Skeet


.NET 4 will be adding a Tuple concept, which deals with this. Unfortunately, the C# language isn't going to provide any language support for "destructuring bind".

like image 44
wekempf Avatar answered Oct 02 '22 20:10

wekempf


Personally, I like the inconvience introduced when using out parameters. It helps me to think about whether my method is really doing what it should be or if I've crammed too much functionality into it. That said, perhaps dynamic typing in C#4.0/.Net 4 will address some of your concerns.

dynamic participant = message.GetParticipants();

var sender = participant.Sender;
var recipient = participant.Recipient;

where

public object GetParticipants()
{
     return new { Sender = ..., Recipient = ... };
}
like image 26
tvanfosson Avatar answered Oct 02 '22 20:10

tvanfosson


You can also return a Tuple<T,U> or something similar. However, since you want to return two string, it might get confusing.

I use the Tuples structs of the BclExtras library which is very handy (found it on SO, thank you JaredPar!).

like image 34
Jabe Avatar answered Oct 02 '22 20:10

Jabe


I don't think such functionality exists, but if it were implemented in a way similar to arrays in perl that could be useful actually.

In perl You can assign an array to a list of variables in parentheses. So you can for example do this

($user, $password) = split(/:/,$data);
like image 24
Michał Piaskowski Avatar answered Oct 02 '22 20:10

Michał Piaskowski


Where this bugs me the most: since there's no overload of (say) DateTime.TryParse that doesn't take an out parameter, you can't write

if (DateTime.TryParse(s, out d))
{
   return new ValidationError("{0} isn't a valid date", s);
}

without declaring d. I don't know if this is a problem with out parameters or just with how the TryParse method is implemented, but it's annoying.

like image 36
Robert Rossney Avatar answered Oct 02 '22 20:10

Robert Rossney


This syntactic sugar is now is now available in the roslyn preview as seen here (called Declaration expressions).

int.TryParse(s, out var x);
like image 31
Craig Avatar answered Oct 02 '22 20:10

Craig