.NET
  Home arrow .NET arrow Page 5 - .NET Type System, Part II
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

.NET Type System, Part II
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 36
    2005-03-28

    Table of Contents:
  • .NET Type System, Part II
  • C# Classes
  • Using Arrays
  • Single-Dimension Arrays
  • Multi-Dimension Arrays
  • C# Strings
  • Boxing Operation

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    .NET Type System, Part II - Multi-Dimension Arrays


    (Page 5 of 7 )

    A single-dimension array is just a simple sequence of elements, but multi-dimension arrays extend to complex sequences. This complexity involves walking in more than one dimension. For example, you can have two dimensions in your array (such as x and y) to represent screen coordinates, so you can go to point "5, 6" or "100, 400" and so on. You specify a multi-dimension array by using a comma inside the square brackets like this: int[,]. If you want a three dimension array, it looks like this: int[,,]. Sometimes multi-dimension arrays are called rectangular arrays, because all the dimensions are fixed, unlike jagged arrays, which are arrays of arrays, as we will discuss soon. The following example illustrates the use of Multi-Dimension arrays:

    using System;

    namespace Arrays
    {
    class SingleDimension
    {
    static void Main(string[] args)
    {
    int[,] points = new int[2,2];
    points[0,0] = 0;
    points[0,1] = 1;
    points[1,0] = 6;
    points[1,1] = 7;

    for(int x=0; x < points.GetLength(0); x++)
    {
    for(int y=0; y < points.GetLength(1); y++)
    {
    Console.Write(points[x,y] + " ");
    }
    Console.WriteLine();
    }
    Console.ReadLine();
    }
    }
    }

    The result will be the following:

    0 1
    6 7

    Note that we have used the method GetLength() to return the length of each dimension. Dimensions are also zero-based. The Length property returns the total number of elements in all dimensions, so we didn't use it in this example.

    Jagged Arrays

    This is the most complex type of array you will define; fortunately, it's very uncommon to define jagged arrays, but we need to discuss it for the sake of completeness. A jagged array is simply array of arrays. In other words, it's much more like a multi-dimension array, but each dimension may vary in its element number from the other dimensions in the array. You declare a jagged array like this:

    int[][] Players = new int[3][];

    This simply means that we have an array called Players that contains three elements. Each element represents an array of its own that can vary in the number of elements. To initialize the elements (the arrays) of the jagged array, you can write the following code:

    int[][] Players = new int[3][];
    Players[0] = new int[4] {1,2,9,10};
    Players[1] = new int[6] {23,12,34,35,65,14};
    Players[2] = new int[11] {1,4,5,6,7,9,10,25,12,15,3};

    As you can see, it's an array of arrays. In our Players jagged array we can store the players' numbers of different games, and because each game contains a different number of players, the jagged array is a perfect solution with which to store them all.

    More .NET Articles
    More By Michael Youssef


     

    .NET ARTICLES

    - Using CrystalReportViewer to Display Crystal...
    - Creating Summary .Net Crystal Reports
    - More on Commands, Input and the WPF
    - Grouping and Aggregating When Querying LINQ ...
    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT