简体   繁体   English

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>'

I would like to do some kind of console framework and for that I need command and arguments for the command.我想做某种控制台框架,为此我需要命令和命令参数。 Here is part of my code:这是我的代码的一部分:

string str = "I do not know how to fix this problem";
List<string> substringList = str.Split().ToList();
List<string> allArgsExcept1st = str[1..^0].ToList();

The input is type string .输入是string类型。 The third line throws an error:第三行抛出错误:

Cannot implicitly convert type 'System.Collections.Generic.List<char>' to 'System.Collections.Generic.List<string>'.无法将类型“System.Collections.Generic.List<char>”隐式转换为“System.Collections.Generic.List<string>”。

I am new to C# so I don't know what to think about this error and how to possibly fix it.我是 C# 新手,所以我不知道如何看待这个错误以及如何修复它。

Thank you.谢谢你。

Based on your latest edit:根据您的最新编辑:

string str = "I do not know how to fix this problem";
List<string> substringList = str.Split().ToList();
List<string> allArgsExcept1st = str[1..^0].ToList();

The problem you have is here:您遇到的问题在这里:

str[1..^0].ToList()

When you are using the range/slice function ( [1..^0] ) on your string you are, in return, getting a string .当您在string上使用 range/slice 函数 ( [1..^0] ) 时,作为回报,您将获得一个string So calling ToList() on that string will give you a List<char> (since a string is also a IEnumerable<char> ) but you are assigning it to a List<string> .所以在该string上调用ToList()会给你一个List<char> (因为一个string也是一个IEnumerable<char> )但你将它分配给一个List<string>

Its unclear what you are ultimately trying to do here, but to fix your problem you just need to change your last line to:目前还不清楚您最终要在这里做什么,但要解决您的问题,您只需将最后一行更改为:

List<char> allArgsExcept1st = str[1..^0].ToList();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 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>' " 无法隐式转换类型System.Collections.Generic.List <char> - cannot implicitly convert type System.Collections.Generic.List<char> 无法将类型&#39;System.Collections.Generic.List&#39;隐式转换为&#39;string&#39; - Cannot implicitly convert type 'System.Collections.Generic.List' to '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 <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<>' 无法隐式转换类型'System.Collections.Generic.List<x> ' 到 'System.Collections.Generic.List<y> '</y></x> - Cannot implicitly convert type 'System.Collections.Generic.List<x>' to 'System.Collections.Generic.List<y>' 无法隐式转换类型&#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>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM