In my C# application I receive a pointer to a C++ struct in callback/delegate. I'm not sure if class
can do the trick, but just casting a C++ pointer to an appropriate C# struct works fine, so I'm using a C# struct for storing data.
Now I want to pass a reference to the struct for further processing.
- I can't use
class
because it probably will not "map" perfectly to the C++ struct. - I don't want to copy the struct for better latency.
How can I do that?
This example demonstrates that struct is passed by value, not by reference:
using System;
namespace TestStruct
{
struct s
{
public int a;
}
class Program
{
static void Main(string[] args)
{
s s1 = new s
{
a = 1
};
Foo(s1);
Console.WriteLine("outer a = " + s1.a);
}
private static void Foo(s s1)
{
s1.a++;
Console.WriteLine("inner a = " + s1.a);
}
}
}
Output is:
inner a = 2
outer a = 1