Note that there are some explanatory texts on larger screens.

plurals
  1. POC# - Syntactic sugar for out parameters?
    text
    copied!<p>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:</p> <pre><code>string sender = message.GetSender(); string receiver = message.GetReceiver(); </code></pre> <p>compacted to:</p> <pre><code>string sender, receiver = message.GetParticipants(); </code></pre> <p>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.</p> <p>Similarly, we have something like this, currently, in C#:</p> <pre><code>string receiver; string sender = message.GetParticipants(out receiver); </code></pre> <p>where the argument to GetParticipants is an <strong>out</strong> 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.</p> <p>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:</p> <pre><code>string sender = message.GetParicipants(out string receiver); </code></pre> <p>to show that receiver was being declared and assigned on the spot.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload