0

The C# compiler doesn't allow this. What's the reason for this? And what workaround I can use?

Basically I need to swap some values around, but don't wanna have the same swapping code all over.

Joan Venge
  • 315,713
  • 212
  • 479
  • 689
  • 1
    Ravadre's answer is solid. If you can provide more details on why you think you need ref treatment of properties, perhaps the crowd can provide you a workaround. – Drew Noakes Oct 02 '09 at 00:18
  • It's because I need to swap some values around. Like x and y for instance. – Joan Venge Oct 02 '09 at 16:09
  • 1
    @Joan: I'd suggest not using automatic properties. This feature was introduce as a syntactic sugar, not as a must-use feature. If situation require that you need to access the variable itself, as well as you property for clean encapsulation, you might consider going "the standard way". – Marcin Deptuła Oct 02 '09 at 16:36

1 Answers1

6

Properties, in general are just methods, so it would be strange if you could just "get reference to them", as no one can be sure, that you are referring to just a variable. I don't think there's a quick and nice workaround other that not using auto-properties in this case.

I'm omitting the idea of using reflection to get to those variables under the hood, as it wouldn't make much sense in this case.

Marcin Deptuła
  • 11,789
  • 2
  • 33
  • 41