+ 1

Simplify return array of chars

This method creates an array of chars which filled with first chars from alphabet but the return row seems too hard. How is it work? Is there any way to turn this line in a simpler form? static char[] createCharArray(int charArrayLength) { return (from c in new char[charArrayLength] select alphabet[0]).ToArray(); }

7th Sep 2016, 6:37 PM
Denis Glotov
Denis Glotov - avatar
1 Antwort
+ 1
What language are you using? If its c# you can store alphabet as a List<char> and then use Linq. return alphabet. Take(charArrayLength).AsArray(). If not then you can always write your own functions to do the same thing if you want to tidy up, but sometimes its better to leave a little extra code than having to provide the outer wrapper.
14th Mar 2017, 9:47 PM
Stuart Robb
Stuart Robb - avatar