Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    API(s)
    Sencha-Touch
    Single Page App
    Bootstrap
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> .NET >> Multidimensional Array In C#.NET
Multidimensional Array In C#.NET
Multidimensional Array In C#.NET


by Awadhendra Tiwari on 1/21/2011 4:00:10 PM

Views: 5555       Comments: 0

Multidimensional Array In C#.NET

When we want to store the information in the format of rows and columns then we use the concept of multidimensional array. Best example of multidimensional array is Microsoft Excel in which all the information is stored in the format of rows and columns. Whenever you want to solve some mathematical problems like matrix, 3-d, 4-d, n-d problems then we also use the concept of multidimensional array. One thing more interesting all the monitors, TVs and LCD’s uses the concept of multidimensional array.

Syntax for Declaring Multidimensional array

<data type> [,] <array name>=new <data type>[<rows>,<columns>];

Where

<data type> represents the data type such as int, float, string etc.

[,]represents dimensions. As much dimensions you wants you have to place so many ,(commos).

[<rows>,<columns>] represents how many rows and columns you need.

Examples for declaring multidimensional array

·         int [,] mat=new int[3,5]; //A multidimensional array mat is declared which have two dimensions.

·         int [, ,] threeD=new int [3,2,4]; //A multidimensional array threeD is declared which have three dimensions.

Inserting Values In Multidimensional Arrays

After declaring multidimensional arrays we have to insert values in multidimensional arrays. In multidimensional arrays each cell is identified by multiple indexes. If we created two dimensional arrays then each cell is identified by two indexes and if we created three dimensions then each cell is identified by three indexes.

Syntax for inserting values in multidimensional array
<array name>[<row index>,<column index>]=<value>;

Where <row index> represents row number and <column index> represents column number.

Example for inserting values in multidimensional arrays

·         mat[0,2]=45; // 45 is stored in mat named array at 0th rows of 2nd columns.

·         threeD[1,2,1]=66; // 66 is stored in threeD named array.

 Inserting/Traversing of array by using for loops

Example 1: Inserting values in multidimensional array.

public void insertMultiDimensionalArray()

        {

            int[,] mat = new int[3, 3];//A multidimensional array of named mat is declared.

            for (int i = 0; i < mat.GetLength(0); i++)//outer loop which iterates rows.GetLength(int Dimension) is method of array which gives the length of dimensions which is passed.

            {

                for (int j = 0; j < mat.GetLength(1); j++)//inner loop which iterates columns.GetLength(int Dimension) is method of array which gives the length of dimensions which is passed.

                {

                    Console.Write("Enter Value In Array--->  ");//A message is declared.

                    mat[i, j] = Convert.ToInt32(Console.ReadLine());//Values is stored in multidimensional array

                }

            }

        }

Example 2: Traversing values in multidimensional array.

public void traverseMultiDimensionalArray(int[,] mat)

        {

            for (int i = 0; i < mat.GetLength(0); i++)

            {

                for (int j = 0; j < mat.GetLength(1); j++)

                {

                    Console.WriteLine("The values are : " + mat[i, j]);

                }

            }

        }

Note: When use concept of multidimensional array then call GetLength(int Dimension) method of array which returns length of the dimensions. Never use Length property of array.

Report Abuse Form
Reason:    
 

Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by Awadhendra TiwariRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 6218
Advertisement
MindStick Cleaner
Advertise with Us
  
Copyright © 2014MindStick. All Rights Reserved.