C# Language Proposal for 'out' Parameters

From: C# Learner (csharp_at_learner.here)
Date: 04/10/04


Date: Sat, 10 Apr 2004 08:04:15 +0100

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

  • [PATCH] epca.c: reformat comments and coding style improvements
    ... static int nbdevs, num_cards, liloconfig; ... This structure is NOT used to overlay the cards physical channel ... static void memwinoff ... static struct channel *verifyChannel ...
    (Linux-Kernel)
  • [PATCH 2/7] dlm: communication
    ... +static struct task_struct *recv_task; ... +static int nodeid_to_addr(int nodeid, struct sockaddr *retaddr) ... +static void lowcomms_data_ready ...
    (Linux-Kernel)
  • [git patches] net driver updates for .27
    ... Add documentation for the driver. ... +static inline int adap2type ... typedef struct board_info { ...
    (Linux-Kernel)
  • [2.6 patch] misc drivers/char/ cleanups
    ... +static int __init espserial_init ... +static struct work_struct genrtc_task; ... -i2cmdSetSeq(unsigned char type, unsigned char size, unsigned char *string) ... static void set_params; ...
    (Linux-Kernel)
  • [PATCH 3/8] dlm: communication
    ... +static struct task_struct *recv_task; ... +static int nodeid_to_addr(int nodeid, struct sockaddr *retaddr) ... +static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port, ...
    (Linux-Kernel)