Search

Wednesday, March 26, 2014
0 comments

Example of array in c# , print array element at specified position

7:01 AMWednesday, March 26, 2014

Introduction

It is a simple example of array in c#, You can print element of array using for loop. Also print array element at specific position or index number. Simple, lower bound initialized from some value, where you want to print array element. Let's take an simple example

Source code

    <form id="form1" runat="server">
    <div>
 
        <asp:Button ID="Button1" runat="server" Text="Button" Width="81px"
            onclick="Button1_Click" />
 
    </div>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </form>

Business Logic Code

    protected void Button1_Click(object sender, EventArgs e)
    {
        string[] fruits = new string[]
        {
            "papaya",
            "Apple",
            "Banana",
            "Grapes",
            "Orange",
            "Pine-apple",  
            "Mango"
        };

        Label1.Text = "fruits array elements.........<br />";
        for (int i = 0; i < fruits.Length; i++)
        {
            Label1.Text += fruits[i] + "<br />";
        }

        Label1.Text += "<br />fruits array elements [index size 1 to 5].........<br />";
        for (int i = 1; i <= 5; i++)
        {
            Label1.Text += fruits[i] + "<br />";
        }

        Label1.Text += "<br />fruits array elements [index size 3 to 5].........<br />";
        for (int i = 3; i <= 5; i++)
        {
            Label1.Text += fruits[i] + "<br />";
        }
 }

Code Generate the following output

Example of array in c# , print array element at specified position
Protected by Copyscape Online Copyright Protection Software

0 comments :

Post a Comment

 
Toggle Footer
Top