char character = \'c\';
string str = null;
str = character.ToString();//this is ok
char[] arrayChar = { \'a\', \'b\', \'c\', \'d\' }
You need to construct a new string.
Doing arrayChar.ToString()
calls the "ToString" method for the char[]
type, which is not overloaded to construct a string out the characters, but rather to construct a string that specifies that the type is an array of characters. This will not give you the behavior that you desire.
Constructing a new string, via str2 = new string(arrayChar);
, however, will give you the behavior you desire.
The issue is that, in C# (unlike C++), a string is not the same as an array of characters. These are two distinctly different types (even though they can represent that same data). Strings can be enumerated as characters (String implements IEnumerable<Char>
), but is not, as far as the CLR is concerned, the same type as characters. Doing a conversion requires code to convert between the two - and the string constructor provides this mechanism.
There is a string constructor that takes a char array.
new string(arrayChar);