C# Tip: LINQ's Enumerable.Range to generate a sequence of consecutive numbers

Read Time:1 Minute, 48 Second

When you need to generate a sequence of numbers in ascending order, you can just use a while loop with an enumerator, or you can use Enumerable.Range.

This method, which you can find in the System.Linq namespace, allows you to generate a sequence of numbers by passing two parameters: the start number and the total numbers to add.

Enumerable.Range(start:10, count:4) 

⚠ Notice that the second parameter is not the last number of the sequence. Rather, it’s the length of the returned collection.

Clearly, it also works if the start parameter is negative:

Enumerable.Range(start:-6, count:3) 

But it will not work if the count parameter is negative: in fact, it will throw an ArgumentOutOfRangeException:

Enumerable.Range(start:1, count:-23) 

⚠ Beware of overflows: it’s not a circular array, so if you pass the int.MaxValue value while building the collection you will get another ArgumentOutOfRangeException.

Enumerable.Range(start:Int32.MaxValue, count:2) 

💡 Smart tip: you can use Enumerable.Range to generate collections of other types! Just use LINQ’s Select method in conjunction with Enumerable.Range:

Enumerable.Range(start:0, count:5) .Select(_ => "hey!"); 

Notice that this pattern is not very efficient: you first have to build a collection with N integers to then generate a collection of N strings. If you care about performance, go with a simple while loop – if you need a quick and dirty solution, this other approach works just fine.

Further readings

There are lots of ways to achieve a similar result: another interesting one is by using the yield return statement:

🔗 C# Tip: use yield return to return one item at a time | Code4IT

This article first appeared on Code4IT 🐧

Wrapping up

In this C# tip, we learned how to generate collections of numbers using LINQ.

This is an incredibly useful LINQ method, but you have to remember that the second parameter does not indicate the last value of the collection, rather it’s the length of the collection itself.

I hope you enjoyed this article! Let’s keep in touch on Twitter or on LinkedIn, if you want! 🤜🤛

Happy coding!

🐧

Source: https://www.code4it.dev/csharptips/enumerable-range

WP Ad Inserter plugin for WordPress

Tag Cloud

Java Java Logical Programs OTP Generation in Java python Recursion youtube video ASCII Upper and Lower Case blockchain javascript graph learn to code software development Successful Software Engineers breadth first search Java Array Programs Java Programs Uncategorized android ios programming kotlin web-development django data sql cybersecurity database swiftui serverless aws swift rust react background-position gradients loader mask grid nth-child pseudo elements indieweb WordPress Print Array without brackets C++ factorial Java String Programs Final Keyword Static Variable Axie Infinity Cryptokitties NFT games tool inserting MISC Tips Codes python code python projects python3 system info python project Bigginers How to Do Integrations Payment Gateways PHP checkout page in php Implement stripe payment gateway in Step by step in PHP integrate stripe gatway in php mysql payment gateway integration in php step by step payment gateway integration in php step by step with source code payment gateway integration in website PHP Integrate Stripe Payment Gateway Tutorial PHP shopping cart checkout code shopping cart in php stripe php checkout PHP/MySQL/JSON best international payment gateway does google pay accept international payments how to accept international payments in india paytm payment gateway razorpay codeigniter github razorpay custom checkout github razorpay get payment details razorpay integration in codeigniter github razorpay international payments Razorpay payment gateway integration in CodeIgniter razorpay payment gateway integration in php code Razorpay payment gateway integration with PHP and CodeIgniter Razorpay payment gateway setup in CodeIgniter Library & Frameworks Tips & Tricks UI/UX & Front-end coding birds online html code for google sign in login with google account in PHP login with google account using javascript login with google account using javascript codeigniter login with google account using php login with google account using php source code
Enhance your READMEs with native Mermaid diagrams Previous post Enhance your READMEs with native Mermaid diagrams
Get notified when there are commits to pull in VS Code Next post Get notified when there are commits to pull in VS Code

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.