简体   繁体   English

C#无法隐式转换类型&#39;System.Collections.Generic.List <string> &#39;到&#39;System.Windows.Documents.List&#39;

[英]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>等,包括非通用IEnumerableIList 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.

相关问题 C#:无法隐式转换类型“System.Collections.Generic.List”<char> &#39; 到 &#39;System.Collections.Generic.List<string> &#39; - C#: Cannot implicitly convert type 'System.Collections.Generic.List<char>' to 'System.Collections.Generic.List<string>' 无法将类型“字符串”隐式转换为“System.Collections.Generic.List”<string> &#39; c# 类 getter - Cannot implicitly convert type 'string' to 'System.Collections.Generic.List<string>' c# class getter 无法将类型&#39;System.Collections.Generic.List&#39;隐式转换为&#39;string&#39; - Cannot implicitly convert type 'System.Collections.Generic.List' to 'string' C# SelectMany 错误“无法隐式转换类型‘System.Collections.Generic.List<char> ' 到 'System.Collections.Generic.List<list> ’”</list></char> - C# SelectMany error " Cannot Implicitly convert type 'System.Collections.Generic.List<char>' to 'System.Collections.Generic.List<List>' " 无法隐式转换类型&#39;System.Collections.Generic.List <AnonymousType#1> 在C#中 - Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1> in C# 无法隐式转换类型&#39;System.Collections.Generic.List <System.Data.DataRow> &#39;到&#39;System.Collections.Generic.List <string> &#39; - Cannot implicitly convert type 'System.Collections.Generic.List<System.Data.DataRow>' to 'System.Collections.Generic.List<string>' 无法隐式转换类型&#39;System.Collections.Generic.list <fooClass> &#39;到&#39;System.Collections.Generic.List <T> - Cannot Implicitly convert type 'System.Collections.Generic.list<fooClass>' to 'System.Collections.Generic.List<T> 无法隐式转换类型&#39;System.Collections.Generic.List <AnonymousType#1> &#39;到&#39;System.Collections.Generic.List - Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List 无法隐式转换类型&#39;System.Collections.Generic.List <MyProduct> &#39;到&#39;System.Collections.Generic.List <T> - Cannot implicitly convert type 'System.Collections.Generic.List<MyProduct>' to 'System.Collections.Generic.List<T> 如何修复“无法将类型System.Collections.Generic.List &lt;&gt;隐式转换为System.Collections.Generic.List &lt;&gt;” - How to fix 'Cannot implicitly convert type System.Collections.Generic.List<> to System.Collections.Generic.List<>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM