code_junkie

In .Net, when if ever should I pass structs by reference for performance reasons

Nov 13th, 2011
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  1. void ManagedMethod(MyStruct[] items) {
  2. foreach (var item in items) {
  3. unmanagedHandle.ProcessOne(item);
  4. }
  5. }
  6.  
  7. void ManagedMethod(MyStruct[] items) {
  8. unmanagedHandle.ProcessMany(items, items.Count);
  9. }
  10.  
  11. // What you have
  12. public struct X
  13. {
  14. public int data;
  15. }
  16. [DllImport("mylib.dll")]
  17. static extern void Foo( ref X arg);
  18.  
  19. // What you could do
  20. [StructLayout(LayoutKind.Sequential)]
  21. public class Y
  22. {
  23. public int data;
  24. }
  25. [DllImport("mylib.dll")]
  26. static extern void Bar( Y arg );
Add Comment
Please, Sign In to add comment