gpt4 book ai didi

c# - 可以简化将字符串转换为 List 的过程吗?

转载 作者:行者123 更新时间:2023-11-30 19:09:51 26 4
gpt4 key购买 nike

为了便于传递参数,我将字符串转换为列表。

这是我的工作示例

string single = "single";

List<string> list = (new [] {single}).ToList();

这可以缩短吗?

此尝试不正确,因为列表变成了字符列表。

List<char> wronglist = single.ToList();

最佳答案

最简单的是:

List<string> list = new List<string>{single};

参见:Collection Initializer - MSDN

Collection initializers let you specify one or more element initializers when you initialize a collection class that implements IEnumerable.

关于c# - 可以简化将字符串转换为 List<string> 的过程吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21765854/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com