Take the 2-minute tour ×

I'm not so experienced in C#, I'm used to work in java. I've already asked for something before and some people advised me this. link to my old topic here But I have new problem with this code. The Visual Studio says that "I cannot have instance filed initializers in struct" - so there is some problem but I really don't understand this thing so is somehow possible make this work?? I just need anchors type Vector3 which is matrix or array of arrays - whatever 4x4 In java I'll probably write it public Vector3[][] = new Vector3[4][4];

This is my problematic code:

    struct BPatch
        public Vector3[][] anchors = new Vector3[][] {new Vector3[4],new Vector3[4],new Vector3[4],new Vector3[4]};
        public uint dlBPatch;// Display list
        public uint texture;// Texture
share|improve this question
possible duplicate of "cannot have instance field initializers in structs" problem –  nawfal Apr 15 '13 at 10:42
add comment

1 Answer

up vote 1 down vote accepted

By the time you've got 5 arrays, you might as well just make it a class. It will behave more sensibly:

class BPatch
    public Vector3[][] Anchors { get { return anchors; } }
    public uint DlBPatch {get;set;}
    public uint Texture {get;set;}

If you have good reason to micro-optimize, a "fixed" array (rather than a jagged array) might be interesting.

share|improve this answer
And if I also need to set the anchors?? Why you used readonly?? I really don't much uderstand difference between fixed and jagged array. I found out that jagged array is array of array - thats only I know. –  user1097772 Dec 31 '11 at 19:41
@user1097772 fixed sized arrays (fixed buffers) are an advanced technique in structs using "unsafe" code to embed a buffer directly in a struct. Hardcore stuff. You could make the outer array read-write. But in most cases that is an error; the intent is usually to change the contents, not to reassign the array. It is your type - do as you will. I should also add: most times, when someone new to c# uses a struct, they use it both inappropriately and incorrectly. Just saying... –  Marc Gravell Dec 31 '11 at 19:47
Thx. I just need to do this: Bpatch bpatch = new Bpatch(); bpatch.anchors[0][1] = new Vector3(-0.75f,-0.75f,-0.5f); bpatch.anchors[0][2] = new Vector3(-0.25f,-0.75f,0.0f); etc.. Is that possible with readonly qualifier? My first intent was have anchors as matrix 4x4 –  user1097772 Dec 31 '11 at 20:19
@user1097772 yes; that doesn't reassign the array - should work fine –  Marc Gravell Dec 31 '11 at 20:30
Thanks a lot ;-) –  user1097772 Dec 31 '11 at 20:42
add comment

Your Answer


