简体   繁体   中英

C# - Creating array where the array value has multiple objects and each one has a value too

I have just recently been doing something in C#, i would like to know how to do something like this.

Array[0] =
  Array['Value'] = 2344;
  Array['LocationX'] = 0;
  Array['LocationY'] = 0;
Array[1] =
  Array['Value'] = 2312;
  Array['LocationX'] = 2;
  Array['LocationY'] = 1;
Array[2] =
  Array['Value'] = 2334;
  Array['LocationX'] = 4;
  Array['LocationY'] = 3;

The data it self its not important, the thing is that i know how to do this in PHP. But in C# i don't, and I've tried some ways and no luck.

In PHP i could just do something like this:

$Array[0]->Value = 2344;
$Array[0]->LocationX = 0;
$Array[0]->LocationY = 0;

And those values would be added to the Array.

In C# i've tried this and doesn't work that way.

Could someone enlighten me in how to do this in C#?

Thanks.

Either write a class or struct to hold Value, LocationX and LocationY.

struct Foo
{
  Foo(value, x, y)
  {
    Value = value;
    LocationX = x;
    LocationY = y;
  }

  Foo() {}

  int Value;
  int LocationX;
  int LocationY;
}

Foo[] f = new [] 
{
  new Foo(1, 2, 3), 
  new Foo(2, 3, 4)
}

or alternatively initialize the array this way:

Foo[] f = new [] 
{
  new Foo() { Value = 1, LocationX = 2, LocationY = 3 },
  new Foo() { Value = 4, LocationX = 5, LocationY = 6 },
}

Or use an Array of Dictionary<string, int> .

Dictionary<string, int>[] array = new []
  {
    new Dictionary<string, int>() {{ "Value", 1 }, {"LocationX", 2}, {"LocationY", 3 }},
    new Dictionary<string, int>() {{ "Value", 4 }, {"LocationX", 5}, {"LocationY", 6 }}
  }

Which is only recommended if it needs to be dynamic (means: you want to have different values in each element of the array or your keys are in strings, not known at compile-time.) Unless it is just hard to maintain.

Well, you could have an array of instances of a class that you write like so:

public class DataForArray
{
    public int Value { get; set; }
    public int LocationX { get; set; }
    public int LocationY { get; set; }
}

Then something like this:

DataForArray[] array = new DataForArray[10];
array[0] = new DataForArray();
array[0].Value = 2344;
etc...

in C#, you can try something like this

// initialize array
var list = new[]
               {
                   new {Value = 2344, LocationX = 0, LocationY = 0},
                   new {Value = 2312, LocationX = 2, LocationY = 4},
                   new {Value = 2323, LocationX = 3, LocationY = 1}
               }.ToList();

// iterate over array
foreach (var node in list)
{
    var theValue = node.Value;
    var thePosition = new Point(node.LocationX, node.LocationY);
}

// iterate over array with filtering ( value > 2300 )
foreach (var node in list.Where(el => el.Value > 2300))
{
    var theValue = node.Value;
    var thePosition = new Point(node.LocationX, node.LocationY);
}

// add again
list.Add(new { Value = 2399, LocationX = 9, LocationY = 9 });

Here is a link that details the use of Multidimensional arrays

http://msdn.microsoft.com/en-us/library/aa288453(VS.71).aspx

You can use anonymous type in C# like that:

var arr = new[] {
    new{Value = 1, LocationX = 2, LocationY = 3},
    new{Value = 1, LocationX = 2, LocationY = 3},
    new{Value = 1, LocationX = 2, LocationY = 3},
    new{Value = 1, LocationX = 2, LocationY = 3},
    new{Value = 1, LocationX = 2, LocationY = 3} };

Only one problem is that properties in anonymous type are read-only. So You can't do something like that:

arr[1].Value = 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM