34

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# struct works fine, so I'm using c# struct for storing data.

Now I want to pass reference to struct for further processing

  • I can't use class because it probably will not "map" perfectly to c++ struct.
  • I don't want to copy 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
svick
  • 225,720
  • 49
  • 378
  • 501
Oleg Vazhnev
  • 22,231
  • 49
  • 161
  • 290
  • 4
    Beware of your reasoning about struct: `struct` in C++ is exactly the same as class (barring default accessibility), while in C# they are completely different - value and reference types. Chances are you really would be better of with classes in C# - at very least read and understand how `struct` behave in C#. – Alexei Levenkov May 17 '13 at 17:27
  • A large number of my interop classes are indeed classes and not structs. If you set up the marshalling correctly (and often the default marshalling works) then you can use a class. If in doubt, I try with a class first. – Matthew Watson May 17 '13 at 17:47

1 Answers1

56

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.

Jon Skeet
  • 1,335,956
  • 823
  • 8,931
  • 9,049
  • 9
    +1. Side note: code handling `struct` must be written very carefully since almost all operations with `struct` create copy (except array, where you still can take a `ref` of an element). – Alexei Levenkov May 17 '13 at 17:25
  • 2
    Why do you strongly recommend against passing structs by `ref`? As far as I can tell, it's the same thing as passing by reference in C++ (eg. `std::string&`), and it can eliminate needless copying of potentially large structs. – Colin Basnett Jun 21 '15 at 21:05
  • 1
    @cmbasnett: I strongly recommended against using mutable structs. Passing a struct by ref is okay, but a) I would advise against large structs in general; b) I would argue against *mutable* structs (as do the Microsoft design guidelines) as they can cause a number of surprises. – Jon Skeet Jun 21 '15 at 21:20