2

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.

Constantine
  • 99
  • 1
  • 10

2 Answers2

2

You can but you have to initialize the array in all constructors because structs require that you assign values to all members in the constructor(s).

 public struct YourStruct
 {
     public char[] arr; 

   public YourStruct(int size)
   {
      arr = new char[size];
   }
 }

Like others have mentioned, if you are creating an ARRAY of OBJECTS (not value types) then a struct is not appropriate to begin with. Look here: How to initialize char array in struct

Community
  • 1
  • 1
T McKeown
  • 12,971
  • 1
  • 25
  • 32
  • i am sorry but, can u please tell me what is wrong with this line of code inside a struct public long LongTest = new long[1000]; – Constantine Jan 26 '14 at 16:24
  • shouldn't it be: public long[] LongTest = new long[1000]; – T McKeown Jan 26 '14 at 16:29
  • yeah it is exactely public long[] LongTest = new long[1000]; but i get this error on compile time. cannot have instance field initializers in structs – Constantine Jan 26 '14 at 16:36
  • i told you already, you need to initialize the array in the constructor not at the declaration. – T McKeown Jan 26 '14 at 16:38
  • i am really thankful to you for your answer and comments but i requested in my question that i want to know why can't we do so otherwise i would have used a class instead. [sorry for bothering you]. – Constantine Jan 26 '14 at 16:48
  • i am sorry but i tried to upvote but it says "needs 15 reputation atleast "unfortunately that is what i am running out of right now – Constantine Jan 26 '14 at 17:02
  • i am sorry i am much novice to stackoverflow than the c# so please tell me how to do so . – Constantine Jan 26 '14 at 17:09
  • my answer should have a check mark to the left, if you click it it should go green. – T McKeown Jan 26 '14 at 17:26
  • i surely will mark it answer, btw is there any chat kind of thing i can have with you about c# questions as i have tons of question – Constantine Jan 26 '14 at 17:43
  • friend my on facebook, I have a small group of software engineer friends that we post Q&A. Tom McKeown in florida. link to the group here: https://www.facebook.com/groups/flswes/ – T McKeown Jan 26 '14 at 17:49
  • Structs are not immutable by specification, though. You should never create a mutable struct but you can do. For example, if you have struct A { int Number; A() { Number = 4 } }, you can still do "A a; a.Number = 6;" and then a.Number will contain 6. – Petr Hudeček Jan 26 '14 at 18:35
  • ok true, they should be immutable, however the properties must be assigned a value upon creation. I have modified my answer, thank you for that. – T McKeown Jan 26 '14 at 18:39
  • Your code won't compile. A structure can't have an explicit parameterless constructor. – Guffa Jan 26 '14 at 18:40
2

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.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005