title: When to use ref and when it is not necessary in C#
body: <p>I have a object that is my in memory state of the program and also have some other worker functions that I pass the object to to modify the state. I have been passing it by ref to the worker functions. However I came across the following function.</p>
  
  <pre><code>byte[] received_s = new byte[2048];
  IPEndPoint tmpIpEndPoint = new IPEndPoint(IPAddress.Any, UdpPort_msg);
  EndPoint remoteEP = (tmpIpEndPoint);
  
  int sz = soUdp_msg.ReceiveFrom(received_s, ref remoteEP); 
  </code></pre>
  
  <p>It confuses me because both <code>received_s</code> and <code>remoteEP</code> are returning stuff from the function. Why does <code>remoteEP</code> need a <code>ref</code> and <code>received_s</code> does not?</p>
  
  <p>I am also a c programmer so I am having a problem getting pointers out of my head.</p>
  
  <p>Edit:
  It looks like that objects in C# are pointers to the object under the hood. So when you pass an object to a function you can then modify the object contents through the pointer and the only thing passed to the function is the pointer to the object so the object itself is not being copied.  You use ref or out if you want to be able to switch out or create a new object in the function which is like a double pointer. </p>