Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How LINQ code generates strings

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report