Loading ...

two-dimensional array in C Sharp | CodeAsp.Net

two-dimensional array in C Sharp

 /5
0 (0votes)

In this blog I will explain how to use two-dimensional array in C#.
Here is my aspx code:-

<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txt2dArray" runat="server" TextMode="MultiLine"></asp:TextBox>
    </div>
    </form>
</body>


Here is my Code behind code:-

namespace Test
{
    public partial class _Default : System.Web.UI.Page
    {
        private string[,] _test = {
                                     {"Name","Mohit"},
                                     {"Sex","Male"},
                                     {"Address","India"},
                                     {"PhNo","100"}
                                 };

        private string str;
        protected void Page_Load(object sender, EventArgs e)
        {
            for(int i=0;i<_test.Length;i++)
            {
                str += _test[i, 0];
                str += " - ";
                str += _test[i, 1];
                str += " , ";
            }
            txt2dArray.Text = str;
        }
    }
}



In above code I am using Length property of the Array.But when i run this Project I will get this following Error.






Here is my solution for this problem:-

namespace Test
{
    public partial class _Default : System.Web.UI.Page
    {
        private string[,] _test = {
                                     {"Name","Mohit"},
                                     {"Sex","Male"},
                                     {"Address","India"},
                                     {"PhNo","100"}
                                 };

        private string str;
        protected void Page_Load(object sender, EventArgs e)
        {
            int _solution = _test.Length/_test.Rank;
            for (int i = 0; i < _solution; i++)
            {
                str += _test[i, 0];
                str += " - ";
                str += _test[i, 1];
                str += " , ";
            }
            txt2dArray.Text = str;
        }
    }
}



Output:-





Happy Coding:)

Comments (no comments yet)

Top Posts