[英]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.