.NET
  Home arrow .NET arrow Page 4 - .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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 - Single-Dimension Arrays


    (Page 4 of 7 )

    This is the basic array type. It has only one dimension, and it's the most common type that you will be using in your applications. Here's an example:

    using System;

    namespace Arrays
    {
    class SingleDimension
    {
    static void Main(string[] args)
    {
    // creating the array
    int[] Numbers = new int[5];

    // initializing the array
    for(int x = 0; x < Numbers.Length; x++)
    {
    Numbers[x] = (x + x);
    }

    // printing the array elements to the Console
    for(int x = 0; x < Numbers.Length; x++)
    {
    Console.WriteLine("Element number {0} = {1}",x, Numbers[x]);
    }

    Console.ReadLine();
    }
    }
    }

    The output of the program will be:

    Element number 0 = 0
    Element number 1 = 2
    Element number 2 = 4
    Element number 3 = 6
    Element number 4 = 8

    This is a very simple example of a single-dimension array. We first create an array of five integer elements, then we loop on the elements to initialize them, and then we do another loop to write them to the Console. Note that we use the Length property of the array (inherited from the base class System.Array) to return the number of elements in the array.

    Let's extend this example. We will not print the element's value in the next example; instead, we will copy the values into a new array using the static method Clone of the base class. Note that this method performs a shallow copying, not a deep copying -- in other words, it will copy value types exactly as you expect (copying the value of the element to the target array's element), but with reference types it will copy the reference itself, not the object that the reference refers to. So, for example,  it will copy the reference aCustomer, not the member fields FirstName and LastName to the new target array's element.

    using System;

    namespace Arrays
    {
    class SingleDimension
    {
    static void Main(string[] args)
    {
    // creating the array
    int[] Numbers = new int[5];

    // initializing the array
    for(int x = 0; x < Numbers.Length; x++)
    {
    Numbers[x] = (x + x);
    }

    // copying the array into a new array
    int[] NewNumbers = (int[])Numbers.Clone();

    //printing the values of the new array
    for(int x = 0; x < NewNumbers.Length; x++)
    {
    Console.WriteLine("NewNumbers's element number {0} = {1}",x, NewNumbers[x]);
    }

    Console.ReadLine();
    }
    }
    }

    We used the Clone() method to copy the values to the newly created array NewNumbers. Note that we must cast to the target array type, because the return type of the clone method is object[] not int[]. We will discuss type conversion in later articles. The result of this code is:

    NewNumber's element number 0 = 0
    NewNumber's element number 1 = 2
    NewNumber's element number 2 = 4
    NewNumber's element number 3 = 6
    NewNumber's element number 4 = 8

    Let's create another example that sorts an array. Copy the following code into a file with the .cs extension, then compile it:

    using System;

    namespace Arrays
    {
    class SingleDimension
    {
    static void Main(string[] args)
    {
    int[] Numbers = {6,3,10,35,2};

    foreach(int i in Numbers)
    {
    Console.WriteLine(i);
    }

    Array.Sort(Numbers);
    Console.WriteLine("After sorting operation");

    foreach(int i in Numbers)
    {
    Console.WriteLine(i);
    }
    Console.ReadLine();
    }
    }
    }
     

    We have used the Array.Sort() method that accepts an array and sorts it; after that we used the foreach structure, which we will discuss later in the series, to iterate through the array and print the value of the elements.

    More .NET Articles
    More By Michael Youssef


     

    .NET ARTICLES

    - Building Applications with Windows Workflow ...
    - Building the Data and Business Layers Using ...
    - The Transformed XML Explorer in MFC
    - List Control and Property Grid with the MFC ...
    - Font, Shell and Masked Edit Controls for MFC
    - Color, Link and Image Editor Controls for M...
    - New Controls for MFC
    - The Windows Ribbon Framework
    - Markup Language for the Ribbon Framework
    - Visually Upgrade Your MFC Project
    - New Features for the Statusbar in MFC
    - Working with the Statusbar in MFC
    - Iron Speed Design v60 Review
    - Binary and XML Serialization
    - Using CrystalReportViewer to Display Crystal...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek