Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Apologies for the short answer, but no, the C# language specification disallows it.</p> <p>See this <a href="https://stackoverflow.com/questions/340528/c-automatic-properties-why-do-i-have-to-write-get-set/340682#340682">answer</a> 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.</p> <p>Hope this helps</p> <p><strong>EDIT: You ask Why?</strong></p> <p>You pass a variable to an <code>out</code> or <code>ref</code> 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.</p> <p>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. </p> <p>Thats a completely different thing to pass to a function than the address of a variable</p> <p>i.e. one variable address v's two function pointers.</p> <p><strong>Update</strong><br> <em>Why doesn't C# just look after this for us?</em></p> <p>I'm no <a href="https://stackoverflow.com/users/88656/eric-lippert">Eric Lippert</a>, but I'll have a go at why</p> <p>What should the signature of the function you're calling be?<br> Lets say you want to call <code>void MyFn(ref int i)</code> should that remain that way, or should it change to say we also allow properties? If it changes to some syntax like <code>void MyFn(prop_ref int i)</code> then this is fairly useless, you can't pass properties to library functions or 3rd party code that wasn't written with the special prop_ref modifier. Anyway I think you're suggesting it shouldn't be different. </p> <p>Now lets say <code>MyFn</code> passes <code>i</code> to a COM function, or WinAPI call, passing the address of <code>i</code> (i.e. outside .net, by ref). If it's a property, how do you get the address of <code>i</code>? There may be no actual int under the property to get the address of. Do you do what VB.Net does? </p> <p>The Vb.Net compiler spots when a property is passed as a ByRef argument to a method. At that point it declares a variable, copies the property to the variable, passes the variable byref and then after the method is called, copies the variable back into the property. i.e.</p> <pre><code>MyFunc(myObject.IntProperty) </code></pre> <p>becomes</p> <pre><code>Dim temp_i As Integer = myObject.IntProperty MyFunc(temp_i) myObject.IntProperty = temp_i </code></pre> <p>Any property side effects don't happen until <code>MyFunc</code> returns, which can cause all sorts of problems and lead to <em>very</em> subtle bugs.</p> <p>In my humble opinion the Vb.Net solution to this problem is also broken, so I'm not going to accept that as an answer.</p> <p>How do you think the C# compiler should handle this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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