Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I am sorry that I do not know what specific problem you are encountering, or how to resolve it [certainly checking namespace is referenced and resolving any conflicts with similarly named methods is a start], but I did notice one or two oddities.</p> <p>Consider the following sample solution,</p> <pre><code>using System.Linq; namespace Sample.Extensions { public static class ByteExtensions { public static void RemoveHeader (this byte[] buffer, byte[] header) { // take first sequence of bytes, compare to header, if header // is present, return only content // // NOTE: Take, SequenceEqual, and Skip are standard Linq extensions if (buffer.Take (header.Length).SequenceEqual (header)) { buffer = buffer.Skip (header.Length).ToArray (); } } } } </code></pre> <p>This compiles and runs in VS2010RC. To demonstrate usage,</p> <pre><code>using Sample.Extensions; namespace Sample { class Program { static void Main (string[] args) { byte[] buffer = new byte[] { 00, 01, 02 }; byte[] header = new byte[] { 00, 01 }; buffer.RemoveHeader (header); // hm, so everything compiles and runs, but buffer == { 00, 01, 02 } } } } </code></pre> <p>So we will not receive a compile or run-time <em>error</em> but clearly it will not operate as intended. This is because extensions must still comply with standard method semantics, meaning parameters are passed by value. We cannot change <code>buffer</code> to point to our new array.</p> <p>We can resolve this issue by rewriting our method to conventional function semantics, </p> <pre><code>public static byte[] RemoveHeaderFunction (this byte[] buffer, byte[] header) { byte[] stripped = null; if (stripped.Take (header.Length).SequenceEqual (header)) { stripped = stripped.Skip (header.Length).ToArray (); } else { stripped = buffer.ToArray (); } return stripped; } </code></pre> <p>Now</p> <pre><code>using Sample.Extensions; namespace Sample { class Program { static void Main (string[] args) { byte[] buffer = new byte[] { 00, 01, 02 }; byte[] header = new byte[] { 00, 01 }; // old way, buffer will still contain { 00, 01, 02 } buffer.RemoveHeader (header); // new way! as a function, we obtain new array of { 02 } byte[] stripped = buffer.RemoveHeaderFunction (header); } } } </code></pre> <p>Unfortunately, arrays are immutable value types [may be using these terms incorrectly]. The only way to modify your "array" in-place is to change the container to a mutable reference-type, like a <code>List&lt;byte&gt;</code>.</p> <p>If you are really keen on "passing by ref", in-place, side-effect semantics, then one option may be the following</p> <pre><code>using System.Linq; namespace Sample.Extensions { public static class ListExtensions { public static void RemoveHeader&lt;T&gt; (this List&lt;T&gt; list, List&lt;T&gt; header) { if (list.Take (header.Count).SequenceEqual (header)) { list.RemoveRange (0, header.Count); } } } } </code></pre> <p>As for usage,</p> <pre><code>static void Main (string[] args) { byte[] buffer = new byte[] { 00, 01, 02 }; byte[] header = new byte[] { 00, 01 }; List&lt;byte&gt; bufferList = buffer.ToList (); // in-place side-effect header removal bufferList.RemoveHeader (header.ToList ()); } </code></pre> <p>Under the hood, <code>List&lt;T&gt;</code> is maintaining an array of type <code>T</code>. At certain thresholds, it is simply manipulating the underlying array and\or instantiating new arrays for us.</p> <p>Hope this helps! :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload