creating a list of consecutive integers in c#

Posted by Alex Bransky on Geeks with Blogs See other posts from Geeks with Blogs or by Alex Bransky
Published on Wed, 28 Apr 2010 18:17:50 GMT Indexed on 2010/04/28 19:27 UTC
Read the original article Hit count: 249

Filed under:

If there's already a way to get a List<int> of consecutive integers without a loop in C#, I don't know what it is, so I created a method for it.

        public static List<int> GetIntegerListFromRange(int start, int end) {
            if (end < start) {
                throw new ArgumentException("Faulty parameter(s) passed: lower bound cannot be less than upper bound.");   
            }
            List<int> returnList = new List<int>(end - start + 1);
            for(int i = start; i <= end; i++) {
                returnList.Add(i);
            }
            return returnList;
        }

 

© Geeks with Blogs or respective owner