title: Most efficient way to pass data from C++ to C#
body: <p>I am looking for the best way to transfer a large amount of data from C++ (struct or a value class?) into a C# class doing as little data copying as possible.   In the sample code below, I have a vector of SubClass objects that has the potential to be very large (10+ million).   So I want to avoid a data copy if possible.</p>
  
  <p>Should I/can I just allocate the objects in GC first and use them directly in c++ and forget about the native c++ structures?  (Performance is my concern with this one.)</p>
  
  <p>Or,  is there some trick that I leverage what is allocated in C++ without causing a data copy?</p>
  
  <p>Here is a sample of something along the lines of what I want to use as a transfer between managed and unmanaged code.</p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;vector&gt;
  struct SubClass {
      std::string DataItem1;
      // lots more here
      std::string DataItem50;
  };
  
  struct Sample {
      int IntValue;
      std::string StringValue;
      std::vector&lt;std::string&gt; SmallList;
      std::vector&lt;SubClass&gt; HugeList;
  };
  </code></pre>
  
  <p>If I can avoid getting into the weeds with pinvoke and COM classes, I would prefer it.</p>