public struct blocks
{
public Int32 xb;
public Int32 yb;
public Int32 size;
};
namespace test
{
class Program
{
static static List
static void Main(string[] args)
{
Int32 index =0;
for (int y = 1; y < 5; y++)
for (int x = 1; x < 5; x++)
{
blocks1[index].xb=x * 2; // store start point (x) of block
blocks1[index].yb = y *2; // store start point (y) of block
blocks1[index].size = 2; // store block size index++; } } } }
VulpesPosted Feb 20, 2014, 5:53 PM
However, if for some reason you don't want to add a constructor, you can also use an 'object initializer' or just set the fields individually which you were already attempting to do in your post.
An array of structs is created in the same way as any other array though the length needs to be known in advance.
If you don't know the length, then you can create a List of structs instead which increases its size dynamically as new elements are added. To add an element to the List, the Add method should be used.
Here are some examples of all this:
// note that when you create an array of structs all fields are automatically
// set to their default values which are 0 for numbers
//print some values