How LINQ code generates strings
This article mainly shows you "how LINQ code generates strings", the content is simple and easy to understand, the organization is clear, I hope to help you solve doubts, let Xiaobian lead you to study and learn "how LINQ code generates strings" this article bar.
This article introduces random sequence generation and string generation. If you have any ideas about LINQ, please share them in the comments.
LINQ code generates random sequences
Here is a simple trick for generating random sequences of length N:
Random rand = new Random(); var randomSeq = Enumerable.Repeat(0, N).Select(i => rand.Next());
Because of LINQ's delay properties, sequences are not computed and stored in arrays, but rather random numbers are generated on demand as randomSeq iterates.
LINQ code generation string
You can also have LINQ code generate strings of various types. For testing or debugging, it is useful to generate strings. Suppose we need to generate a string of length N, in the form "ABCABCABC." With LINQ, the solution is elegant:
string str = new string( Enumerable.Range(0, N) .Select(i => (char)('A' + i % 3)) .ToArray()); //Petar Petrov gives another interesting way to generate strings using LINQ: string values = string.Join(string.Empty, Enumerable.Repeat(pattern, N).ToArray Above is "How LINQ code generates strings" All the contents of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!