C# String ToCharArray() Method

The ToCharArray() method in C# is used to convert a string into an array of characters. It returns a new array of characters that contains the same characters as the string.

The syntax of the ToCharArray() method is as follows:

char[] charArray = string.ToCharArray();
So‮‬urce:www.theitroad.com

For example, to convert a string to an array of characters, you can use the following code:

string text = "Hello World";
char[] charArray = text.ToCharArray();
foreach (char c in charArray)
{
    Console.WriteLine(c);
}

In this example, the ToCharArray() method is used to convert the string "Hello World" to an array of characters. The resulting array is assigned to the charArray variable. The foreach loop is then used to iterate over the array and print each character to the console using the WriteLine() method.

Note that the ToCharArray() method does not modify the original string. Instead, it returns a new array of characters that represents the same characters as the string. If you need to modify the characters in the string, you can convert the string to a char array, modify the array, and then create a new string from the modified array using the String(char[]) constructor.