Here is the code:

string input = "hello world"; string output = new string(input.ToCharArray().Reverse().ToArray()); 

Here I created the input variable and the output variable. I wanted to assign output inverted value to input , why without .ToArray() code does not work, why I need .ToArray() and what it does I do not understand.

    2 answers 2

    The Reverse method (like most other Linq methods) returns IEnumerable , but the String class does not have a constructor that takes an IEnumerable<char> , but there is a constructor that takes an array. The ToArray method ToArray array based on the input IEnumerable sequence.

    String constructor

    By the way, the string implements IEnumerable<char> , so you can call Reverse directly on it, ToCharArray can be not called.

      As an addition to the correct answer @Andrey NOP, if your goal is to expand a line, you need a more complex technique .

      For example, this code supports characters from the high Unicode planes, as well as unnormalizable accents:

       static IEnumerable<string> GetGraphemeClusters(string s) { var enumerator = StringInfo.GetTextElementEnumerator(s); while (enumerator.MoveNext()) yield return (string)enumerator.Current; } static void Main(string[] args) { string[] strings = { "Les Mise\u0301rables", "Co\u0323\u0302ng ho\u0300a xa\u0303 ho\u0323\u0302i chu\u0309 nghi\u0303a Vie\u0323\u0302t Nam", "😂" }; foreach (string input in strings) { string output = string.Concat(GetGraphemeClusters(input).Reverse().ToArray()); Debug.WriteLine($"{input} -> {output}"); } } 

      (code borrowed from here )