block1 works:-
String input = "The quick brown fox jumps over the lazy dog";
char[] output = input.Reverse().ToArray();
Console.WriteLine(output);
when I try to store the output into a new String, block2 works as well:-
String input = "The quick brown fox jumps over the lazy dog";
char[] output = input.Reverse().ToArray()
String output2 = new String(output);
Console.WriteLine(output2);
But when I try to store the output into a String, block3 does not works:-
String input = "The quick brown fox jumps over the lazy dog";
char[] output = input.Reverse().ToArray()
String output2 = output; //I tried to use convert.ToString() as well, but it didn't work
Console.WriteLine(output2);
why does block 2 works and block 3 does not ??
CodePudding user response:
why does block 2 works and block 3 does not ??
block 2 uses the constructor of string that accepts a char array. This is an existing constructor that Microsoft have provided that forms a new string
block 3 would require there to be an implicit conversion defined between a char array and a string, because the output from Reverse().ToArray()
is an array of char, not a string. It is important to appreciate that strings can be treated as IEnumerable<char>
, and you can run LINQ methods like Reverse
on them, but because they're treated as sequences of char on the way in, they come out as sequences of char, not strings. You cannot directly assign a char array to a string variable because Microsoft have provided no such conversion
You might find this discussion on SESE interesting and relevant
Footnote; you asked why you cannot call ToString
on a char array- again, there just isn't an overload of ToString
on a char[]
that turns the char array into a string, because Microsoft haven't provided one.
There are plenty enough ways of making a char[]
into a string
(your block 2 is ideal) that Microsoft don't need to spend the time and effort to make another, and you have to also consider there might be code out there that relies on someCharArray.ToString()
returning System.Char[]
- not everyone knows you can do myObject is char[]
, they might have done myObject.ToString == "System.Char[]"
to check if the object is a char array.
Whenever a language is upgraded with new features, every possible problem that the new feature could cause, will be investigated. The risk of adding .ToString()
to char[]
that produces a string from the characters, and having it silently (because it won't cause a compile error) break potentially millions of codebases around the world is just too great, when there isn't any benefit over what you can do already:
var chars = new[]{'H','e','l','l','o',' ','W','o','r','l','d'}
var s = chars.ToString(); //produces "System.Char[]"
var s = new string(chars); //produces "Hello World"
^
one extra keystroke
It only costs one more keystroke to use the constructor version, and if you use modern c# that doesn't require stating the type after the new, so long as the type is on the other side, it's shorter even than the older var
version too :)
var s = chars.ToString();
string s = new(chars);