Introduction
The Generation operators help in creating a new sequence of values. The Generation operators are DefaultlfEmpty, Empty, Range, and Repeat. The DefaultlfEmpty clause replaces an empty collection with a default single collection. The Empty clause refers to an empty collection. The Range clause generates a collection that contains a sequence of numbers. The Repeat clause generates a collection that contains at least one repeated value.The syntax of Range clause is:
For C#
public static IEnumerable<int> Range( int start, int count);
The Range clause throws an ArgumentOutOfRangeException exception if the count is less than 0, or if the start + or count -1 parameters are larger than the maximum value.
The syntax of the Empty clause is:
For C#
public static IEnumerable<T> Empty<T>();
The Empty clause caches a single empty sequence of the given type.
Lets take an simple example
<form id="form1" runat="server"><div>
<asp:ListBox ID="ListBox1" runat="server" Height="254px" Width="153px">
</asp:ListBox>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="odd and even " />
</div>
</form>
Code Behind
protected void Button1_Click(object sender, EventArgs e)
{
var num = from n in Enumerable.Range(1, 20)
select new { Number = n, oddevennumber = n % 2 == 1 ? "odd" : "even" };
foreach (var item in num)
{
ListBox1.Items.Add("The number is " + item.Number + item.oddevennumber);
}
}
Code Generate the following output