2

Is it possible to do workaround for Automatic property of Reference Type (Class) to be able passed by Ref?

The only thing I think about is not to use Automatic property: just declare private variable as always and passing it by ref + adding public property aside if I need.
Frankly it looks a bit stupid ( all that bureaucracy needed to bypass it)

My Questions:
1.Why the compiler don't allow automatic property to be pass by ref(like it was a simple data member)? 2.When do we use or should use Ref? I noticed some people saying that using it may occur only in rare scenarios. What are them?

JavaSa
  • 5,813
  • 16
  • 71
  • 121
  • I think you have a misunderstanding surrounding what is *really* happening. Read this [blog by Jon Skeet](http://www.yoda.arachsys.com/csharp/parameters.html). – Mike Perrenoud May 20 '14 at 17:06
  • Second question is likely duplicate of [Why use ref keyword](http://stackoverflow.com/questions/186891/why-use-ref-keyword-when-passing-an-object) - please read and see if you have more questions (don't ask 2 questions in one - use new post for each) – Alexei Levenkov May 20 '14 at 17:10
  • That's not the case, I've already read this and understood it. This dosen't answer my first question, about the second its providing detailed information of how pass by ref works ( both for Reference type and value type), but still do not answer when should we use the Ref key word. --> Currentely I know how to do something with approch of passing by ref (Reference type) and also doing it without passing by ref. – JavaSa May 20 '14 at 17:11
  • 1
    What;s your use case for passing a property by reference? There's probably an alternate solution. – D Stanley May 20 '14 at 17:11

2 Answers2

9

Why the compiler don't allow automatic property to be pass by ref

because it's not a reference - it's a method pair (get/set). The workaround is to create a local variable that hold's the property's value, pass that by reference, and set the property to the updated value:

var temp = myObj.myProperty;
MethodWithRefParam(ref temp);
myObj.MyProperty = temp;

As you said, you could pass the backing property by reference, but only from within that class.

When do we use or should use Ref?

Typically ref (and out, which is a special case of ref) parameters are only used when returning the value is not practical. TryParse is a common framework example. Instead of returning the parsed value, the method returns a bool telling you whether the parse succeeded or not, and "returns" the parsed value in an out parameter.

Using ref with reference types is usually a code smell that could indicate the author doesn't understand how reference types work.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • adding this link also for future readers http://stackoverflow.com/questions/1381881/c-sharp-reference-type-still-needs-pass-by-ref – JavaSa May 20 '14 at 17:31
1

You use automatic properties because it allows you eventually to change how property is implemented without impacting dependent code. There is no special marking of any kind on property that it was implemented as "automatic". Even if you look at IL of the method you will only find that it is a property with backing field.

As result compiler can't make any assumptions if this property is autogenerated and treats all properties as regular ones.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179