【问题标题】:Mapping Objects to 2D array using C#使用 C# 将对象映射到二维数组
【发布时间】:2021-05-04 05:13:21
【问题描述】:
public class Item {
  public int id;
  public int price, stock;
  public Item(int id, int price, int stock) {
    id = this.id;
    price = this.price;
    stock = this.stock;
  }
}

static void Main(string[] args) {
  Item first = new Item(1, 23, 2);
  Item second = new Item(2, 345, 5);
  
  var myArray = new Array[50,50];
  myArray[0,1] = first;
  myarray[0,2] = second;
}

如何将我的 Item 对象映射到我的二维数组中?无法将类型“csharp.Program.Item”隐式转换为“System.Array”

【问题讨论】:

    标签: c# arrays multidimensional-array


    【解决方案1】:

    代替

    var myArray = new Array[50,50];
    

    使用

    var myArray = new Item[50, 50];
    

    这将创建一个 Item 数组,让您无需任何转换即可将值映射到数组。

    【讨论】:

    • 谢谢你,我知道我错过了什么!
    猜你喜欢
    • 2019-12-08
    • 2011-01-10
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    • 2016-11-22
    相关资源
    最近更新 更多