无法动态设置数组初始值设定项长度

Can't dynamically set array initializer length

我正在使用 C# 连接到 FedEx API,我对如何修改一些现有代码以满足我的需要有点困惑。包含的代码片段是他们固定代码的一部分,他们在其中示例了如何使用一种商品。但是,当我的代码运行时,我将有 n 种商品......我不确定如何动态解决这个问题。

举个例子,如果我有 3 种商品,我手动添加 3 个 new Commodity() 语句就可以了。然而,这需要是动态的。

request.RequestedShipment.CustomsClearanceDetail.Commodities = new Commodity[3] { 
  new Commodity(), 
  new Commodity(), 
  new Commodity() 
};

这是方法的开始,第一行是我需要帮助的地方。之后,我认为根据数组大小将其他所有内容放入循环中应该没问题。

request.RequestedShipment.CustomsClearanceDetail.Commodities = new Commodity[1] { 
  new Commodity() 
};

request.RequestedShipment.CustomsClearanceDetail.Commodities[0].NumberOfPieces = "1";
request.RequestedShipment.CustomsClearanceDetail.Commodities[0].Description = "Books";
request.RequestedShipment.CustomsClearanceDetail.Commodities[0].CountryOfManufacture = "US";
//
request.RequestedShipment.CustomsClearanceDetail.Commodities[0].Weight = new Weight();
request.RequestedShipment.CustomsClearanceDetail.Commodities[0].Weight.Value = 1.0M;
request.RequestedShipment.CustomsClearanceDetail.Commodities[0].Weight.Units = WeightUnits.LB;

如果我只是这样做,我会得到一个错误:

request.RequestedShipment.CustomsClearanceDetail.Commodities = new Commodity[3] { 
  new Commodity() 
};

"An array initializer of length 3 is expected"

首先,您根本不必提及 13;输入 [] 并让系统为您计算所需的长度:

// Commodity[3] will be created
request.RequestedShipment.CustomsClearanceDetail.Commodities = new Commodity[] { 
  new Commodity(), 
  new Commodity(), 
  new Commodity() 
};

// Commodity[1] will be created
request.RequestedShipment.CustomsClearanceDetail.Commodities = new Commodity[] { 
  new Commodity()
};

如果你想创建一个大小为 n 的数组,你可以尝试 Linq:

using System.Linq;

...

int n = 3;

request.RequestedShipment.CustomsClearanceDetail.Commodities = Enumerable
  .Range(0, n) 
  .Select(index => new Commodity())
  .ToArray();

甚至

int n = 3;

// 3 equivalent commodities
request.RequestedShipment.CustomsClearanceDetail.Commodities = Enumerable
  .Range(0, n) 
  .Select(index => new Commodity() {
     NumberOfPieces = "1", 
     Description = "Books",
     CountryOfManufacture = "US",
     Weight = new Weight() {
       Value = 1.0M,
       Units = WeightUnits.LB
     }
   })
  .ToArray(); 

@Dmitry 先生,上面的回答几乎涵盖了所有内容......但是,下面的部分也可以写成以下部分:

你的原码

request.RequestedShipment.CustomsClearanceDetail.Commodities = new Commodity[1] { 
  new Commodity() 
};

request.RequestedShipment.CustomsClearanceDetail.Commodities[0].NumberOfPieces = "1";
request.RequestedShipment.CustomsClearanceDetail.Commodities[0].Description = "Books";
request.RequestedShipment.CustomsClearanceDetail.Commodities[0].CountryOfManufacture = "US";
//
request.RequestedShipment.CustomsClearanceDetail.Commodities[0].Weight = new Weight();
request.RequestedShipment.CustomsClearanceDetail.Commodities[0].Weight.Value = 1.0M;
request.RequestedShipment.CustomsClearanceDetail.Commodities[0].Weight.Units = WeightUnits.LB;

可修改如下:

修改动态len数组和属性初始化代码

request.RequestedShipment.CustomsClearanceDetail.Commodities = new Commodity[] { 
   new Commodity {
      NumberOfPieces = "1",
      Description = "Books",
      CountryOfManufacture = "US",
      Weight = new Weight{
            Value = 1.0M,
            Units = WeightUnits.LB
      },
      // add more properties... as per requirement
   },
   // add more objects... as per requirement
};