Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you are looking to use C for a performance gain, most likely you are planning to do so through the use of pointers. C# does allow for use of pointers, using the unsafe keyword. Have you considered that?</p> <p>Also how will you be calling this code.. will it be called often (e.g. in a loop?) If so, marshalling the data back and forth may more than offset any performance gains.</p> <hr> <p><strong>Follow Up</strong></p> <p>Take a look at <a href="http://www.vsj.co.uk/dotnet/display.asp?id=174" rel="nofollow noreferrer">Native code without sacrificing .NET performance</a> for some interop options. There are ways to interop without too much of a performance loss, but those interops can only happen with the simplest of data types. </p> <p>Though I still think that you should investigate speeding up your code using straight .NET.</p> <hr> <p><strong>Follow Up 2</strong></p> <p>Also, may I suggest that if you have your heart set on mixing native code and managed code, that you create your library using c++/cli. Below is a simple example. Note that I am not a c++/cli guy, and this code doesn't do anything useful...its just meant to show how easily you can mix native and managed code.</p> <pre><code>#include "stdafx.h" using namespace System; System::Collections::Generic::List&lt;int&gt; ^MyAlgorithm(System::Collections::Generic::List&lt;int&gt; ^sourceList); int main(array&lt;System::String ^&gt; ^args) { System::Collections::Generic::List&lt;int&gt; ^intList = gcnew System::Collections::Generic::List&lt;int&gt;(); intList-&gt;Add(1); intList-&gt;Add(2); intList-&gt;Add(3); intList-&gt;Add(4); intList-&gt;Add(5); Console::WriteLine("Before Call"); for each(int i in intList) { Console::WriteLine(i); } System::Collections::Generic::List&lt;int&gt; ^modifiedList = MyAlgorithm(intList); Console::WriteLine("After Call"); for each(int i in modifiedList) { Console::WriteLine(i); } } System::Collections::Generic::List&lt;int&gt; ^MyAlgorithm(System::Collections::Generic::List&lt;int&gt; ^sourceList) { int* nativeInts = new int[sourceList-&gt;Count]; int nativeIntArraySize = sourceList-&gt;Count; //Managed to Native for(int i=0; i&lt;sourceList-&gt;Count; i++) { nativeInts[i] = sourceList[i]; } //Do Something to native ints for(int i=0; i&lt;nativeIntArraySize; i++) { nativeInts[i]++; } //Native to Managed System::Collections::Generic::List&lt;int&gt; ^returnList = gcnew System::Collections::Generic::List&lt;int&gt;(); for(int i=0; i&lt;nativeIntArraySize; i++) { returnList-&gt;Add(nativeInts[i]); } return returnList; } </code></pre>
 

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