title: Is it safe to pass a struct with "ref this" to native code
body: <p>I am currently integrating the Steamworks SDK into my game and have a couple of methods which require to pass a struct as a pointer, for example:</p>
  <pre class="lang-c prettyprint-override"><code>// Public interface method declared by the library
  S_API bool SteamAPI_SteamNetworkingIPAddr_IsLocalHost( SteamNetworkingIPAddr* self );
  </code></pre>
  <pre class="lang-cs prettyprint-override"><code>// C# implementation
  
  [StructLayout(LayoutKind.Explicit, Size = 18, Pack = 1)]
  public struct SteamNetworkingIPAddr {
  
      // My current usage of &quot;ref this&quot;
      public bool IsLocalhost =&gt; NativeMethods.SteamAPI_SteamNetworkingIPAddr_IsLocalHost(ref this);
  
      // How another library solves it
      public bool IsLocalhost {
          get {
              SteamNetworkingIPAddr copy = this;
              NativeMethods.SteamAPI_SteamNetworkingIPAddr_IsLocalHost(ref self);
          }
      }
  
  
      private static class NativeMethods {
          [DllImport(SteamAPI.LIBRARY_NAME, EntryPoint = &quot;SteamAPI_SteamNetworkingIPAddr_IsLocalHost&quot;, CallingConvention = CallingConvention.Cdecl)]
          internal static extern bool SteamAPI_SteamNetworkingIPAddr_IsLocalHost(ref SteamNetworkingIPAddr self);
      }
  }
  
  </code></pre>
  <p>I am a bit concerned about passing <code>this</code> with <code>ref</code> to the native method because I've seen other implementations which create a copy of the struct before passing it but I could not find something which officially says that it's either safe or unsafe to do so.</p>
  <p>So, my question is - should I make a copy or keep my solution?</p>