Sample test code - why is it bringing back an extra value

Camillia
Camillia used Ask the Experts™
on
I'm practicing some online code examples for my potential interviews and I came across one code sample --> pass in 2 integers and bring back odd numbers between the range, inclusive.

Why is this bringing back 11? it shouldn't

Or, if I pass 96 and 97

List<int> list = new List<int>();

			int l = 3;
			int r = 9;

			List<int> t = Enumerable.Range(l, r).ToList();
			var oddNumbers = t.Where(num => num % 2 != 0);

			foreach (int i in oddNumbers)
				list.Add(i);

			var res = list;

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
The 2nd parameter to Enumerable.Range says how many numbers to generate, not the maximum number. Therefore, the largest value in t is 12, which 11 is less than.
Oh, I made a mistake in it  I was just looking up Range. Thanks. That explains it.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial