.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  
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 - 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

    - 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 1 hosted by Hostway
    Stay green...Green IT