Re: Why properties cannot be used as ref or out arguments

Barry Kelly <[email protected]> Mon, 10 Mar 2008 11:04:41 +0000
Newsgroups gmane.comp.windows.devel.dotnet.clr
Message-ID <[email protected]>
Dean Cleaver <[email protected]> wrote:

> I can understand ref arguments, because they might not be set on
> passing, but I can't figure out why they can't be used as out =
arguments?

You've read the other stuff which indicates the why.

You might consider passing anonymous methods instead, though. It ought
to be fairly concise with the new lambda syntax. Something like:

---8<---
using System;

class App
{
    static void Main()
    {
        FrobRef(() =3D> MyProp, x =3D> MyProp =3D x);
        FrobOut(x =3D> MyProp =3D x);
    }
   =20
    delegate T Getter<T>();
    delegate void Setter<T>(T value);
   =20
    static void FrobRef(Getter<int> g, Setter<int> s)
    {
        s(g() * 2);
    }
   =20
    static void FrobOut(Setter<int> s)
    {
        s(42);
    }
   =20
    static int MyProp
    {
        get
        {
            Console.WriteLine("Reading");
            return 12;
        }
        set
        {
            Console.WriteLine("Writing: {0}", value);
        }
    }
}
--->8---

You might even "improve" this by creating a simple PropertyRef class
with a single property Value, that delegates getting and setting to the
one or two delegates passed to its constructor, etc.

-- Barry

--=20
http://barrkel.blogspot.com/

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
This list is hosted by DevelopMentor=AE  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com