Re: C# Language Proposal for 'out' Parameters

From: Abubakar (emailallrise_at_yahoo.com)
Date: 04/10/04


Date: Sat, 10 Apr 2004 14:29:25 +0500


>Also, I understand why "ref" and "out" are needed in the method
declaration,
>but it seems superfluous to have to specify them at every call.
i think it makes the code more understandable, for example while debugging
you wont have to goto to see the method declaration to find out if the
parameter was a ref or out, instead you'll know right there and then from
the call what the methods accepts.

"Bruno Jouhier [MVP]" <bjouhier@club-internet.fr> wrote in message
news:OcSyHqtHEHA.3276@TK2MSFTNGP09.phx.gbl...
> I have a better proposal for ref and out:
>
> instead of using "pass by reference" semantics, why not use
> "copyin/copyout" semantics for "ref" and "copyout"semantics for "out".
>
> This way, you would not need to declare a variable every time. In lots of
> cases, you would be able to receive the value directly into an object
> property. For example:
>
> if (TryGetValue(myObj.MyProp))
> {
> // doSomething
> }
>
> This would be equivalent (almost) to the following that you have to write
> today:
>
> int i;
> if (TryGetValue(out i))
> {
> myObj.MyProp = i;
> }
>
> For a "ref" (in/out) parameter, the gain is even more obvious. It would
save
> both the assignment from the property to a temp variable before the call,
> and the reverse assignment after the call.
>
> Also, I understand why "ref" and "out" are needed in the method
declaration,
> but it seems superfluous to have to specify them at every call.
>
> The last refinement would be to have normal parameters (no keyword, "in"
> semantics) be "readonly" inside the method body (you can do this in Java
by
> marking them with "final"). Of course this is probably too far fetched as
it
> goes against the C/C++/Java tradition, but IMO, this is so much cleaner
and
> would make the language easier to understand to beginners (for once,
> mimicking Pascal or ADA rather than C++ would help).
>
> Just some ideas...
>
> Bruno.
>
>
>
> "C# Learner" <csharp@learner.here> a écrit dans le message de
> news:ewGyWosHEHA.3556@TK2MSFTNGP10.phx.gbl...
> > Note
> > ----
> >
> > Please use a fixed-width font to view this, such as Courier New.
> >
> >
> >
> >
> >
> > Problem
> > -------
> >
> > When passing a parameter with the modifier 'out', it is necessary to
> > write two statements:
> >
> > - one for the declaration of the variable due to receive the value; and
> > - one for the method call, where the variable is passed as a parameter.
> >
> > Example:
> >
> > static void Main()
> > {
> > int i;
> > if (TryGetValue(out i)) {
> > Console.WriteLine("Value is: {0}.", i);
> > }
> > }
> >
> > This is inconsistent with the traditional way of returning values from a
> > method.
> >
> > Example:
> >
> > static void Main()
> > {
> > int i = GetValue();
> > Console.WriteLine("Value is: {0}.", i);
> > }
> >
> >
> >
> >
> >
> > Solution
> > --------
> >
> > The solution to this problem would be to allow the declaration of the
> > variable to appear within the statment where it's being passed as an
> > 'out' parameter.
> >
> > Example:
> >
> > static void Main()
> > {
> > if (TryGetValue(out int i)) {
> > Console.WriteLine("Value is: {0}.", i);
> > }
> > }
> >
> >
> >
> >
> >
> > Benefits
> > --------
> >
> > (a) Method bodies can benefit from one less statement for each time this
> > technique is used, promoting readability.
> >
> > (b) C# becomes more consistent since returning values using both the
> > traditional way, and using 'out' parameters, is uniform. Again,
> > this promotes readability.
> >
> > Example:
> >
> > // traditional way
> > static void Main()
> > {
> > int i = GetANumber();
> > DoSomethingWith(i);
> > }
> >
> > // using an 'out' parameter, with the aforementioned syntax
> > static void Main()
> > {
> > if (TryGetANumber(out int i)) {
> > DoSomethingWith(i);
> > }
> > }
> >
> >
> >
> >
> >
> > End
> > ---
> >
> > Comments?
> >
> > Regards,
> > C. S. Learner
>
>



Relevant Pages