Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can declare arrays in struct?

Tags:

c#

interop

How can I declare structure with a fixed size array in it?

I found solution, but it only works for primitive data-types. I need my array to be of type MyStruct.

So how can I declare a struct with an array of other structs in it?

ex.

    unsafe struct Struct1{
      fixed int arrayInt[100]; // works properly 
      fixed Struct2 arrayStruct[100]; //not compile
    }
like image 436
Code Ars Avatar asked Nov 08 '10 11:11

Code Ars


1 Answers

My colleague found the working way to do this. I think it`s right way.

    [StructLayout(LayoutKind.Sequential)]
     public struct Struct1
     {
           [MarshalAs(UnmanagedType.ByValArray, SizeConst = sizeOfarray)]
           private Struct2[] arrayStruct;
     }
like image 103
Code Ars Avatar answered Sep 22 '22 03:09

Code Ars