Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-创建数组,其中数组值有多个对象,每个对象也有一个值_C#_Arrays - Fatal编程技术网

C#-创建数组,其中数组值有多个对象,每个对象也有一个值

C#-创建数组,其中数组值有多个对象,每个对象也有一个值,c#,arrays,C#,Arrays,我最近在C#做一些事情,我想知道如何做这样的事情 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['Loca

我最近在C#做一些事情,我想知道如何做这样的事情

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;
数据本身并不重要,问题是我知道如何在PHP中实现这一点。但在C#中,我没有,我也尝试过一些方法,但没有成功

在PHP中,我可以这样做:

$Array[0]->Value = 2344;
$Array[0]->LocationX = 0;
$Array[0]->LocationY = 0;
DataForArray[] array = new DataForArray[10];
array[0] = new DataForArray();
array[0].Value = 2344;
etc...
这些值将被添加到数组中

在C#中,我尝试过这种方法,但不起作用

有人能告诉我如何用C#做这件事吗


谢谢。

好吧,您可以像这样编写一个类的实例数组:

public class DataForArray
{
    public int Value { get; set; }
    public int LocationX { get; set; }
    public int LocationY { get; set; }
}
然后像这样:

$Array[0]->Value = 2344;
$Array[0]->LocationX = 0;
$Array[0]->LocationY = 0;
DataForArray[] array = new DataForArray[10];
array[0] = new DataForArray();
array[0].Value = 2344;
etc...

好的,您可以有一个类的实例数组,您可以这样编写:

public class DataForArray
{
    public int Value { get; set; }
    public int LocationX { get; set; }
    public int LocationY { get; set; }
}
然后像这样:

$Array[0]->Value = 2344;
$Array[0]->LocationX = 0;
$Array[0]->LocationY = 0;
DataForArray[] array = new DataForArray[10];
array[0] = new DataForArray();
array[0].Value = 2344;
etc...

编写一个类或结构来保存Value、LocationX和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)
}
或者,也可以通过以下方式初始化阵列:

Foo[] f = new [] 
{
  new Foo() { Value = 1, LocationX = 2, LocationY = 3 },
  new Foo() { Value = 4, LocationX = 5, LocationY = 6 },
}
或者使用
字典的数组

Dictionary[]array=new[]
{
新字典(){“Value”,1},{“LocationX”,2},{“LocationY”,3},
新字典(){“Value”,4},{“LocationX”,5},{“LocationY”,6}
}

只有当它需要动态时才建议使用它(意味着:您希望数组的每个元素中都有不同的值,或者您的键在字符串中,在编译时是未知的),除非它很难维护。

或者编写一个类或结构来保存Value、LocationX和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)
}
或者,也可以通过以下方式初始化阵列:

Foo[] f = new [] 
{
  new Foo() { Value = 1, LocationX = 2, LocationY = 3 },
  new Foo() { Value = 4, LocationX = 5, LocationY = 6 },
}
或者使用
字典的数组

Dictionary[]array=new[]
{
新字典(){“Value”,1},{“LocationX”,2},{“LocationY”,3},
新字典(){“Value”,4},{“LocationX”,5},{“LocationY”,6}
}

只有当它需要动态时才建议使用它(意味着:您希望在数组的每个元素中都有不同的值,或者您的键在字符串中,在编译时是未知的),除非它很难维护。

这里有一个链接详细介绍了多维数组的使用


这里有一个链接,详细介绍了多维数组的使用

在C#中,你可以试试这样的东西

// 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 });
在C#中,你可以试试这样的东西

// 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 });

您可以在C#中使用匿名类型,如下所示:

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} };
arr[1].Value = 2
唯一的问题是匿名类型的属性是只读的。所以你不能这样做:

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} };
arr[1].Value = 2

您可以在C#中使用匿名类型,如下所示:

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} };
arr[1].Value = 2
唯一的问题是匿名类型的属性是只读的。所以你不能这样做:

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} };
arr[1].Value = 2

您想对数据结构做什么?for循环将到达另一个数组中的每个元素。执行一些筛选,然后添加到此数组。没有什么特别的,只是那些需要理解数组的“hello worlds”。一旦我开始学习C#并且不知道怎么做,我想你们可以帮忙。我们仍然不知道你们想做什么。数组本身不是端点。只是指完成调用函数或将数据输出到网页之类的事情。对于这两个方面,ASP.Net的结构都比数组好。您试图对数据结构做什么?for循环将指向另一个数组中的每个元素。执行一些筛选,然后添加到此数组。没有什么特别的,只是那些需要理解数组的“hello worlds”。一旦我开始学习C#并且不知道怎么做,我想你们可以帮忙。我们仍然不知道你们想做什么。数组本身不是端点。只是指完成调用函数或将数据输出到网页之类的事情。ASP.Net在这两方面的结构都比数组好。@Hellfrost:没错,但它与php实现非常接近。使用duck类型更安全。@Hellfrost:不,不是。它绝对清晰,绝对定义,绝对使用静态类型。这不是动态类型。这也不是“duck-typing”。@Hellfrost:没错,但它与php实现非常接近。使用duck类型更安全。@Hellfrost:不,不是。它绝对清晰,绝对定义,绝对使用静态类型。这不是动态类型。这也不是“鸭子打字”。