String数组类型方法返回类型错误

public String[][] GetAllItems() { FoodCityData.ShoppingBuddyEntities fdContext = new FoodCityData.ShoppingBuddyEntities(); IQueryable Query = from c in fdContext.Item select c; List AllfNames = Query.ToList(); int arrayZise = AllfNames.Count; String[,] xx = new String[arrayZise,2]; int i = 0; int j = 0; foreach(Item x in AllfNames) { xx[i,0] = x.ItemName.ToString(); xx[i, 1] = x.ItemPrice.ToString(); i++; } return xx[2,2]; // how do i write return type? } 

我在这段代码段返回类型中遇到错误。 我能知道如何正确地编写这种方法吗?

你有Jagged Array返回类型,你需要返回二维Rrectangular数组 ,你可以像这样返回二维数组。

 public String[,] GetAllItems() { //your code String[,] xx = new String[arrayZise,2]; //your code return xx; } 

当您尝试返回多维数组时,您的方法假设返回Jagged Array

将方法签名修改为:

 public String[,] GetAllItems() 

目前你的方法返回一个字符串,这就是错误的原因。

您正在返回一个string但您的返回类型是string[][] 。 我想你要做的是返回一个string[,]

 public string[,] GetAllItems() { ... return xx; } 

你的方法返回类型是锯齿状数组 ,因为你的xx[2,2]是一个string ,你的方法返回简单的string ,这就是你得到错误的原因。

只需返回两个相同的arrays ;

 public String[,] GetAllItems() { ..... return xx[2,2]; }