linq - How to get element at a specified index or a default value

Linq ElementAtOrDefault Operator - How to get the element at a given index in a sequence or a default value if the index is out of range
LinqElementAtOrDefaultOperator.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Linq" %>

<!DOCTYPE html>
<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        string[] fruits = { "AnĂ³n", "Annona reticulata", "Atte", "Bramble fruit" };

        string elementAtOrDefaultIndex1 = (from f in fruits
                                           select f).ElementAtOrDefault(1);

        string elementAtOrDefaultIndex5 = (from f in fruits
                                           select f).ElementAtOrDefault(5);

        Label1.Text = "Fruits List:....... <br />";
        foreach (string names in fruits)
        {
            Label1.Text += names.ToString() + "<br />";
        }

        Label2.Text = "After Calling ElementAtOrDefault(1) Operator: " + elementAtOrDefaultIndex1;
        Label2.Text += "<br /><br />After Calling ElementAtOrDefault(5) Operator: " + elementAtOrDefaultIndex5;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Linq ElementAtOrDefault Operator - How to get the element at a given index in a sequence or a default value if the index is out of range</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:DarkBlue; font-style:italic;">
            Linq Standard Query Operator: Element Operator - ElementAtOrDefault
            <br />How to get the element at a given index in a sequence or
            <br /> a default value if the index is out of range
        </h2>
        <hr width="600" align="left" color="CornFlowerBlue" />
        <asp:Label
             ID="Label1"
             runat="server"
             Font-Size="X-Large"
             ForeColor="DarkOrchid"
             Font-Italic="true"
             >
        </asp:Label>
        <br />
        <asp:Label
             ID="Label2"
             runat="server"
             Font-Size="X-Large"
             ForeColor="MediumBlue"
             Font-Italic="true"
             >
        </asp:Label>
        <br /><br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Test Linq Query Operator - ElementAtOrDefault"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
More c# linq examples