4

As per this post, the reason there is a distinction between ref and out is because it is costly to copy the value of the variable when using ref.

Why is there a need to marshall in the first place? Doesn't C# just pass the pointer under the hood? In that case, there would be no need to copy values.

Community
  • 1
  • 1
  • 1
    Can you explain why you mention marshalling – David Heffernan Dec 22 '13 at 20:48
  • In answer you linked author said about marshaling to different process, so pointers are no longer relevant, because each process has it's own virtual memory. – Oleh Nechytailo Dec 22 '13 at 20:49
  • @OlehNechytailo: I am referring to one Windows process. I don't think the author meant actually different Windows processes. – user3126319 Dec 22 '13 at 20:52
  • @DavidHeffernan The post linked mentions it. – user3126319 Dec 22 '13 at 20:52
  • @user3126319 From answer you linked: "It makes a big difference when the data needs to be marshalled e.g. to another process, which can be costly." It means when data need to be marshalled, ref implies sending previous value, when out doesn't. Also take a look on all Related questions at right sidebar, they have pretty good answers. – Oleh Nechytailo Dec 22 '13 at 20:56
  • @OlehNechytailo Is there ever a need to marshall a ref value other than copying one value from one process to another? – user3126319 Dec 22 '13 at 20:58
  • 1
    @user3126319 Don't supply the details by referring to some other post. Please ask your question here. – David Heffernan Dec 22 '13 at 20:58
  • All the related links explain the difference between ref and out. None of them really explain why there is a distinction. – user3126319 Dec 22 '13 at 21:00
  • Semantic difference, compiler checks, marshalling behavior, all of that are explained there. – Oleh Nechytailo Dec 22 '13 at 21:01
  • @DavidHeffernan That's exactly why I did. The only reason I referred to the other post is because you asked why I mentioned marshall. Is there any reason I shouldn't mention marshall? – user3126319 Dec 22 '13 at 21:02
  • @OlehNechytailo I figured there would be a more significant reason behind this design decision other than the semantic difference (which I don't really find helpful) and the case that you need to copy one variable from one process to another. – user3126319 Dec 22 '13 at 21:05
  • @user3126319 Mention marshalling if you wish. Are you therefore only interested in the difference between `ref` and `out` for pinvoke? – David Heffernan Dec 22 '13 at 21:06
  • @DavidHeffernan: Yes, I am interested in that if it explains the design decision to have both `ref` and `out` in C#. – user3126319 Dec 22 '13 at 21:10

1 Answers1

3

Because the semantics of the two are completely different.

An out parameter is used to indicate that it will be used to return (output) a value, nothing more.

A ref parameter on the other hand indicates that an existing object (variable) should be passed to the method by reference. In the context of C#, an object passed by reference (not to be confused by reference types) is often a hint that the method will (and should) modify that object. It shouldn't be used "just because." It is generally used only for value types since it is the only way to get reference semantics for them.

Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
  • The semantics of the two are not so different as C# would pretend. Passing a field to real `out` parameter should leave the field unmodified until such time as the method returns; instead, a field passed as an `out` parameter will behave just like one passed as `ref`, except that the compiler will insist that variables must be assigned before they can be passed as `ref`, and will assume that variables passed as `out` have been assigned. – supercat Feb 21 '15 at 00:27