pass c# struct by reference?

后端 未结 1 948
旧巷少年郎
旧巷少年郎 2020-12-15 15:56

In my c# application i receive pointer to c++ struct in callback/delegate. I\'m not sure if class can do the trick but just casting c++ pointer to appropriate c

相关标签:
1条回答
  • 2020-12-15 16:35

    It sounds like you just want to use ref to pass the struct by reference:

    private static void Foo(ref s s1)
    {
        s1.a++;
        Console.WriteLine("inner a = " + s1.a);
    }
    

    And at the call site:

    Foo(ref s1);
    

    See my article on parameter passing in C# for more details.

    Note that other than for interop, I would normally strongly recommend against using mutable structs like this. I can understand the benefits here though.

    0 讨论(0)
提交回复
热议问题