C# 错误“无法隐式转换类型”System.Collections.Generic.IEnumerable'到“System.Collections.Generic.List”
我正在尝试将数字列表转换为字符串,然后稍后再转换为列表。转换为字符串可以很好地工作,但当我将其转换回列表时,会出现错误,无法隐式转换类型“System.Collections.Generic.IEnumerable”。这是我正在运行的代码C# 错误“无法隐式转换类型”System.Collections.Generic.IEnumerable'到“System.Collections.Generic.List”,c#,C#,我正在尝试将数字列表转换为字符串,然后稍后再转换为列表。转换为字符串可以很好地工作,但当我将其转换回列表时,会出现错误,无法隐式转换类型“System.Collections.Generic.IEnumerable”。这是我正在运行的代码 using System; using System.Collections.Generic; using UnityEngine; using System.Linq; using System.Globalization; public class Te
using System;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using System.Globalization;
public class Test : MonoBehaviour
{
void Start()
{
Function();
}
void Function()
{
List<Int32> listBuffer= new List<Int32>();
System.Random rnd = new System.Random();
for (int x = 1; x < 100; x++)
{
listBuffer.Add(x);
}
List<Int32> listInt= (from item in listBuffer
orderby rnd.Next()
select item).ToList<Int32>();
print(listInt[0]);
string stringBuffer= String
.Join(
", ",
listInt.Select(n => n.ToString(CultureInfo.InvariantCulture))
.ToArray()
);
print(stringBuffer);
List<Int32> listInt2= stringBuffer
.Split(',')
.Select(c => Int32.Parse(c, CultureInfo.InvariantCulture));
print(listInt2[0]);
}
}
这应该可以解决这个问题:
List<Int32> listInt2 = stringBuffer
.Split(',')
.Select(c => Int32.Parse(c, CultureInfo.InvariantCulture)).ToList();
而不是:
List<Int32> listInt2 = stringBuffer
.Split(',')
.Select(c => Int32.Parse(c, CultureInfo.InvariantCulture));
我不知道我怎么会错过。非常感谢。