[英]C# Cannot implicitly convert type 'System.Collections.Generic.List<string>' to 'System.Windows.Documents.List'
I want to make a method that returns a list. 我想做一个返回列表的方法。 But when I declare the datatype string tot the list it gives me this error:
但是当我将数据类型字符串声明为列表时,它给了我这个错误:
Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Windows.Documents.List'
无法将类型'System.Collections.Generic.List'隐式转换为'System.Windows.Documents.List'
at the return
statement. 在
return
声明中。
private List StockCheck(List<string> ListStockCheck, string[] StringProducts, int[,] productstock)
{
for (int product = 0; product < StringProducts.GetLength(0); product++)
{
for (int dag = 0; dag < this.productStock.GetLength(1); dag++)
{
sumP[product] += this.productStock[product, dag];
}
if(sumP[product] >=115 && sumP[product] <= 190)
{
ListVoorraadCheck.Add("De voorraad van " + StringProducts[product] + " is goed!");
}
}
return ListStockCheck;
}
It shows the error on the return ListStockCheck;
它在
return ListStockCheck;
显示错误return ListStockCheck;
statement. 声明。
Does anyone know how to fix this? 有谁知道如何解决这一问题?
Well you actually don't need to return anything at all as far as I can see. 好吧,据我所知,您实际上根本不需要返回任何东西。 List is not used at all in the method and if it is, you are returning a parameter, so you have it already since you have to pass it to the method.
方法中根本没有使用列表,如果使用列表,则返回的是参数,因此已经有了它,因为必须将其传递给方法。 As one of the commentator has written it's not necessary in C# to return something, you can write
private void MyMethod()
, then you don't need a return. 正如一位评论员所写的那样,无需在C#中返回任何内容,您可以编写
private void MyMethod()
,然后就不需要返回。
There can be some cases when you want that like Fluent interface, but it does not look like it's what you want here. 在某些情况下,您可能需要类似Fluent的界面,但是看起来好像不是您想要的。
If you still want to return it you need to change the return type to be the same as parameter, eg List<string>
. 如果仍要返回,则需要将返回类型更改为与参数相同的类型,例如
List<string>
。
You can use other types like eg IEnumerable<string>
, IList<string>
, etc. including non-generic IEnumerable
and IList
. 您可以使用其他类型,例如
IEnumerable<string>
, IList<string>
等,包括非通用IEnumerable
和IList
。 It depends on what you want to achieve which type to return. 这取决于您要实现返回哪种类型。
List<T>
is a generic Type, Using generic type requires parameters. List<T>
是通用类型,使用通用类型需要参数。 Since your are returning ListStockCheck
I am assuming you want to return list of string ie List<string>
. 由于您正在返回
ListStockCheck
所以我假设您要返回字符串列表,即List<string>
。 Thus your return type should be something like below 因此,您的返回类型应如下所示
private List<string> StockCheck(List<string> ListStockCheck, string[] StringProducts, int[,] productstock)
{
// Other code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.