We can convert a character to a string in C# using the ToString()
method. It converts a character value to its equivalent string representation.
public static string ToString (char c);
c
: The character we want to change to string.
The value returned is a string representation of the specified character.
We will convert some characters to strings in the code below using the ToString()
method:
// use Systemusing System;// create classclass HelloWorld{// main methodstatic void Main(){// create characterschar c1 = 'Y';char c2 = 'E';char c3 = 'S';// conver to stringsstring s1 = Char.ToString(c1);string s2 = Char.ToString(c2);string s3 = Char.ToString(c3);// print returned valueConsole.WriteLine(s1);Console.WriteLine(s2);Console.WriteLine(s3);}}
From the code above, we convert the characters to strings using the ToString()
method.
line 10
to line 12
we create some character variables and create initials for them.line 15
to line 17
we convert the characters to strings using the ToString()
method.line 20
to line 22
we print the results.