Generation Operators

Range

This sample uses Range to generate a sequence of numbers from 100 to 149 that is used to find which numbers in that range are odd and even.

Code:

            public void Linq65()
            {
                var numbers = Enumerable.Range(100, 50).Select(n => new {Number = n, OddEven = n%2 == 1 ? "odd" : "even"});

                foreach (var n in numbers)
                {
                    Log.WriteLine("The number {0} is {1}.", n.Number, n.OddEven);
                }
            }

Result:

Repeat

This sample uses Repeat to generate a sequence that contains the number 7 ten times.

Code:

            public void Linq66()
            {
                var numbers = Enumerable.Repeat(7, 10);

                foreach (var n in numbers)
                {
                    Log.WriteLine(n);
                }
            }

Result: