title: How can I check if two Span<T> intersect?
body: <p>Consider the following function:</p>
  
  <pre><code>public static bool TryToDoStuff(ReadOnlySpan&lt;byte&gt; input, Span&lt;byte&gt; destination) {
      ...
  }
  </code></pre>
  
  <p>This function returns whether it was able to "do stuff" on <code>destination</code> based on <code>input</code>'s content.</p>
  
  <p>I'd like to check if the <strong>memory regions "wrapped" by</strong> <code>input</code> and <code>destination</code> intersect, and if so, throw an exception, since this would corrupt <code>input</code>'s data. <strong>How can I do that (without reflection or unsafe code)?</strong></p>
  
  <p>I know I could write some xmldoc and warn the users that the parameters should not intersect, but that's a poor man's solution.</p>
  
  <p>Edit: for those asking for examples, Wazner's examples are on point.</p>
  
  <pre><code>// On arrays
  Span&lt;byte&gt; firstArray = new byte[10];
  Span&lt;byte&gt; secondArray = new byte[10];
  Intersects&lt;byte&gt;(firstArray.Slice(5, 5), firstArray.Slice(3, 5)); // Should throw
  Intersects&lt;byte&gt;(firstArray.Slice(5, 5), secondArray.Slice(3, 5)); // Should not throw
  
  // And on stackallocated memory
  Span&lt;byte&gt; firstStack = stackalloc byte[10];
  Span&lt;byte&gt; secondStack = stackalloc byte[10];
  Intersects&lt;byte&gt;(firstStack.Slice(5, 5), firstStack.Slice(3, 5)); // Should throw
  Intersects&lt;byte&gt;(firstStack.Slice(5, 5), secondStack.Slice(3, 5)); // Should not throw
  </code></pre>