无法将简单列表统一转换为字符串

时间:2018-07-25 00:19:29

标签: c# unity3d

此代码在wpf上正常工作。

在我的代码中list1可以工作,但是如果将字符串转换为char列表并再次更改为string,则显示错误:无法从char []转换为string []。 需要建议。

这是代码:

        string x = "hello";

        List<char> list = x.ToList<char>();

        //error
        string combindedString = string.Join(",", list.ToArray());

        string xz = string.Join("", list);
        //---

        //okay....
        List<string> list1 = new List<string>()
                                {
                                    "Red",
                                    "Blue",
                                    "Green"
                                };

        string output = string.Join("", list1.ToArray());

enter image description here

在wpf中:

enter image description here

2 个答案:

答案 0 :(得分:1)

我怀疑您的问题的根源仍然在于使用旧的.Net Framework,我认为它是Unity 3.5的默认设置。您可以选择一些方法来解决此问题:

  • 您可以定位一个更新框架,我相信Unity在实验模式下最多支持4.6.1。

  • 您可以解决将char[]转换为字符串的问题。

  • 或仅使用字符串构造函数:

一个简单的例子:

[Test]
public void StringTest()
{
    string x = "hello";
    List<char> list = x.ToList<char>();
    string newStr = new string(list.ToArray());
    Assert.IsTrue(newStr == x);
}

答案 1 :(得分:-1)

可以很容易地通过循环来实现。             字符串x =“ hello”

console.log (Object)
console.log(typeof Object)

// call the function to create an object
let o = Object()
// o's prototype is the Object prototype
console.log (o.__proto__ === Object.prototype)