-->

Tuesday, March 25, 2014

Print element of array at specific position in c#, LINQ subset array

Print element of array at specific position in c#, LINQ subset array

Using the LINQ subset array you can print element of array at specific position through Skip( ) method. If you want to print array element at position number 3 then you should skip three starting index of array. Like
ArrayName .Skip(3).Take(2).ToArray( ) .

Source

<form id="form1" runat="server">
    <div>  
        <asp:Button ID="Button1" runat="server" Height="33px" onclick="Button1_Click"
            Text="Button" Width="96px" />  
    </div>
    <p style="width: 118px">
        <asp:Label ID="Label1" runat="server" Height="40px" Text="Label" Width="100px"></asp:Label>
    </p>
    </form>

Business Logic Code

 protected void Button1_Click(object sender, EventArgs e)
    {
        string[] Vegetables = new string[]
        {
            "Tomato",
            "Potato",
            "Onion",
            "Carrot",
            "Sweet-Potato",
            "Locky"

        };

        Label1.Text = "Vegetables array...<br />";
        foreach (string Vegetable in Vegetables)
        {
            Label1.Text += Vegetable + "<br />";
        }
        string[] arraySubset = Vegetables.Skip(3).Take(2).ToArray();

        Label1.Text += "<br />Vegetables sub array [element after 3 and count 2]...<br />";
        foreach (string Vegetable in arraySubset)
        {
            Label1.Text += Vegetable + "<br />";
        }

Print element of array at specific position in c#, LINQ subset array

Read other related articles

Also read other articles

© Copyright 2013 Computer Programming | All Right Reserved