Search code examples
c#-4.0managed

why can't we define array in structures in C#


Hi all kindly help me to understand why cant we define Arrays in structs in c#

i tried the other asked question but most of them suggests to use class instead of a struct. that is ok for implementation purpose but i want to understand why can't we define arrays in structs.


Solution

  • You can have an array in a structure, but that is quite pointless in most cases.

    An array is an object, so the structure will only contain a reference for an array. If you create an array and assign to the reference, the array is created on the heap. The usual reason for using a structure is to avoid creating objects on the heap.