-2

This code works fine on wpf.

In my code list1 will work but if convert a string to a list of char and change again to string it showing error of : can't convert from char[] to string []. need suggestion.

here's the code :

        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

in wpf:

enter image description here

bulliedMonster
  • 236
  • 1
  • 8
  • 18

2 Answers2

2

Well I suspect the source of your issue still lies with using the older .Net Framework that is default with Unity, 3.5 I believe. You have some options, to work around the problem though:

  • You can target an update framework, I believe Unity supports up to 4.6.1 in experimental mode.

  • You can work around converting a char[] to string.

  • Or just use the string constructor:

A simple example:

[Test]
public void StringTest()
{
    string x = "hello";
    List<char> list = x.ToList<char>();
    string newStr = new string(list.ToArray());
    Assert.IsTrue(newStr == x);
}
JSteward
  • 6,833
  • 2
  • 21
  • 30
-1

This can easily be achieved with a loop. string x = "hello"

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

        string originalword = "";

        foreach (var letter in list)
        {
            originalword += letter.ToString();
        }

        // original word is equal to hello
  • 1
    Thanks a lot for a nice answer. However its okay but I'm concerned about performance. – bulliedMonster Jul 25 '18 at 00:42
  • 1
    Another way you might try is to convert the list into an array and then create a string like so: string original = new string(array); Not sure how well that would do on performance though :) You didn't mention that in your post – Zachary Watson Jul 25 '18 at 00:50