Search code examples
c#marshallinglow-level

Are Non blittable structs treated as reference type?


If I have a non blittable struct, how it is treated by the compiler/CLR?

For example: if I have a non blittable struct X which I pass without the 'ref/out' keyword to some method, what actually will be passed to this method? A pointer to the actual allocated struct on the managed heap or the compiler will make a copy of the struct's instance onto the stack when passing to the method?

Thanks.


Solution

  • Blittable and Non-Blittable types are only relevant in the context of marshaling. When passing structs to managed methods, the regular rules of value types apply, meaning a copy is made.

    In case you want to wrap your original struct, which is what you want judging from your comments, the option you discussed is to use a Tuple, the only downside I see from this is readability: Tuple.Item1/Item2 may be unclear.

    Another option is to make a wrapper class that would contain your struct as its only property:

    class StructWrapper
    {
         YourStruct Value { get; set; }
    }