代码之家  ›  专栏  ›  技术社区  ›  Fábio Antunes

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

  •  0
  • Fábio Antunes  · 技术社区  · 16 年前

    我最近在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#中,我没有,我也尝试过一些方法,但没有成功。

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

    这些值将被添加到数组中。

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

    谢谢

    5 回复  |  直到 16 年前
        1
  •  5
  •   David M    16 年前

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

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

    然后像这样:

    DataForArray[] array = new DataForArray[10];
    array[0] = new DataForArray();
    array[0].Value = 2344;
    etc...
    
        2
  •  5
  •   Stefan Steinegger    16 年前

    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<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 }}
      }
    

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

        3
  •  3
  •   Anwar Chandra    16 年前

    // 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 });
    
        4
  •  0
  •   G-Man    16 年前

    下面是一个详细介绍多维数组使用的链接

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

        5
  •  0
  •   Edison Chuang    16 年前

    您可以在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