1

This is going to be an embarrassing question for me, but they say there is no worse question that one not asked. So I am maintaining someone else code in C#. Almost no problem there but there is something that is bothering me. There are several structs of two types:

private struct Struct1
{
    public string name; 
    public int numX; 
    public int[] numA;  
    public int[,] x; //x coordinate
    public int[,] y; //y coordinate
}
private struct struct2
{
    public string name; 
    public double[] coA; 
    public double[] coB; 
    public double[] coC; 
    public double[] coD;
}
Struct1[] arrayOfS1;
Struct1 exOfS1;
Struct2[] arrayofS2;
Struct exOfS2;

They seem quite similar right. After that in the Form Load function we instantiate them

int k;
for(k=0;k<MAX_ENTRY;k++)
{
    arrayOfS1[k].numA = new int[MAX_BOUNDARY];
    arrayOfS1[k].x = new int[MAX_BOUNDARY, MAX_POINT];
    arrayOfS1[k].y = new int[MAX_BOUNDARY, MAX_POINT];
}
exOfS1.numA = new int[MAX_BOUNDARY];
exOfS1.x = new int[MAX_BOUNDARY, MAX_POINT];
exOfS1.y = new int[MAX_BOUNDARY, MAX_POINT];

and something similar for the other struct. But my problem is:before that we do this:

arrayOfS1=new Struct1[MAX_ENTRY];
exOfS1= new Struct1(); //<------WHY THIS!!?
arrayOfS2= new Struct2[MAX_ENTRY];
//exOfS2=new Struct2(); //<----Why NOT this???

As you can see the array of structs are instantiated with new as they should be. In both cases. But for the variables that hold only one of the structs, only the first one (exOfS1) is instantiated with new, and not the second one. In any case, my question would be Do exOfS1 and exOfS2 need to be created with new??

In the code I have only one is created with new and I wonder why? Is it unnecessary?

Muhammed Shevil KP
  • 1,404
  • 1
  • 16
  • 21
KansaiRobot
  • 7,564
  • 11
  • 71
  • 150
  • 2
    I'm not sure I see the difference between the two lines of 'Why this' and 'Why not this', apart from the struct type? – Rob Feb 02 '17 at 05:08
  • 1
    I assume this line `arrayOfS1= new Struct2[MAX_ENTRY];` should be: `arrayOfS2= new Struct2[MAX_ENTRY];`? – JanR Feb 02 '17 at 05:09
  • yes sorry, I edited it – KansaiRobot Feb 02 '17 at 05:12
  • 1
    I think this question sums it up:http://stackoverflow.com/questions/9207488/what-does-the-keyword-new-does-to-a-struct-in-c – JanR Feb 02 '17 at 05:13
  • @Rob Exactly, I don't see any difference. So I am guessing the author made a mistake and my question is, is new necessary (for both variables)? – KansaiRobot Feb 02 '17 at 05:19
  • @KansaiRobot It really depends what you're going to *do* with the variables `exOfS1` and `exOfS2` – Rob Feb 02 '17 at 05:21
  • 1
    as far as I am aware `new` should only be required if you have a constructor that does something special to the struct, if there is no constructor you should be able to use it without `new` keyword. – JanR Feb 02 '17 at 05:45
  • The first rule of structs is that you should only use them when you KNOW you need one. If you don't know for certain, then you don't need one. – satnhak Feb 02 '17 at 06:11
  • I certainly need those structs, and plus is code that I am maintaining. The thing is I suspected that new was not necessary (as the answers suggest) – KansaiRobot Feb 02 '17 at 07:03
  • _they say there is no worse question that one not asked._ Hm. Well, actually it is: not asked or asked without doing some research before..! – TaW Feb 02 '17 at 08:53

2 Answers2

0

Structures are value types and they are initialized with default values for all their fields. Thus, you don't need to use new keyword to create a structure - it has already been created if its a field of a class.

bottaio
  • 4,963
  • 3
  • 19
  • 43
0

To make it even a bit more confusing, new is actually not needed because struct is value type:

struct a { public int i; }

a b, c;
b.i = 1;
c.i = 2;
Debug.Print(b.i + "," + c.i); // "1,2"
Slai
  • 22,144
  • 5
  • 45
  • 53