title: `fixed` vs GCHandle.Alloc(obj, GCHandleType.Pinned)
body: <p>I tried to find out how pinned pointers defined with <code>fixed</code> keyword work. My idea was that internally <code>GCHandle.Alloc(object, GCHandleType.Pinned)</code> was used for that. But when I looked into the IL generated for the following C# code:</p>
  
  <pre><code>unsafe static void f1()
  {
    var arr = new MyObject[10];
    fixed(MyObject * aptr = &amp;arr[0])
    {
       Console.WriteLine(*aptr);
    }
  }
  </code></pre>
  
  <p>I couldn't find any traces of <code>GCHandle</code>.</p>
  
  <p>The only hint I saw that the pinned pointer was used in the method was the following IL declaration:</p>
  
  <pre><code>.locals init ([0] valuetype TestPointerPinning.MyObject[] arr,
                [1] valuetype TestPointerPinning.MyObject&amp; pinned aptr)
  </code></pre>
  
  <p>So the pointer was declared as pinned, and that did not require any additional methods calls, to pin it.</p>
  
  <p>My questions are</p>
  
  <ul>
  <li>Is there any difference between using pinned pointers in the declaration and pinning the pointer by using <code>GCHandle</code> class?</li>
  <li>Is there any way to declare a pinned pointer in C# without using <code>fixed</code> keyword? I need this to pin a bunch of pointers within a loop and there's no way I can do this using fixed keyword.</li>
  </ul>