views:

767

answers:

3

Hi All,

Can I pass a property as an "out" or "ref" parameter if not then why not?

e.g.

Person p = new Person();

. . .

public void Test(out p.Name);
+9  A: 

Apologies for the short answer, but no, the C# language specification disallows it.

See this answer to another question to see what happens when you try. It also says why you shouldn't make the property just be a public field to get around the restriction.

Hope this helps

EDIT: You ask Why?

You pass a variable to an our or ref parameter you're actually passing the address (or location in memory) of the variable. Inside the function the compiler knows where the variable really is, and gets and writes values to that address.

A property looks like a value, buts it's actually a pair of functions, each with a different signature. So to pass a property, you'd actually need to pass two function pointers, one for the get, and one for the set.

Thats a completely different thing to pass to a function than the address of a variable

i.e. one variable address v's two function pointers.

Binary Worrier
+1  A: 

Instead, you should do something like this

WhatEverTheType name;

Test(out name);

// Choose one of the following construction

Person p = new Person();
p.Name = name;

Person p = new Person(name);
Person p = new Person(Name => name);
Fabian Vilers
+3  A: 

Others have explained that you can't do this in C#. In VB.NET, you can do this, even with option strict/explicit on:

Option Strict On
Option Explicit On
Imports System.Text

Module Test

   Sub Main()
       Dim sb as new StringBuilder
       Foo (sb.Length)
   End Sub

   Sub Foo(ByRef x as Integer)
   End Sub

End Module

The above code is equivalent to this C# code:

using System.Text;

class Test
{
     static void Main()
     {
         StringBuilder sb = new StringBuilder();
         int tmp = sb.Length;
         Foo(ref tmp);
         sb.Length = tmp;
     }

     static void Foo(ref int x)
     {
     }
}

Personally I'm glad that C# doesn't have this - it's muddying the waters quite a lot, particularly in terms of the value of the property if the parameter is set within the method but then an exception is thrown.

EDIT: As requested, my reasoning as to why I believe passing properties in muddies the waters. If you pass a normal variable by reference, then that variable is evaluated every time it is referenced within the method. If the value changes for some reason (e.g. as a side-effect of some other work in the method) then that change will be immediately visible in the method. That's not the case if you pass a property by reference in VB.NET: the property getter is invoked once, and then the property setter is invoked once. It's not like you're passing in "here's a property - get and set from that whenever you use the parameter."

Here's a full example where passing a field and passing an entirely trivial property in .NET have very different results:

Option Strict On
Option Explicit On
Imports System.Text

Class Test

   Dim counter as Integer

   Property CounterProperty As Integer
       Get
           Return counter
       End Get
       Set (ByVal value as Integer)
           counter = value
       End Set
   End Property

   Sub Increment
       counter += 1
   End Sub

   Shared Sub Main()
       Dim t as new Test()
       Console.WriteLine("Counter = {0}", t.counter)
       t.Foo(t.counter)
       Console.WriteLine("Counter = {0}", t.counter)

       t.CounterProperty = 0
       Console.WriteLine("CounterProperty = {0}", t.CounterProperty)
       t.Foo(t.CounterProperty)
       Console.WriteLine("CounterProperty = {0}", t.CounterProperty)
   End Sub

   Sub Foo(ByRef x as Integer)
       x = 5
       Increment
       Increment
       Increment
       x += 1
   End Sub

End Class
Jon Skeet
Jon: If you don't mind, could you elucidate your thoughts on “muddying the waters”, I'm not getting why you think passing properties into ref parameters are is a particularly bad idea (I'm not saying they're a good idea). Thanks mate.
Binary Worrier