Creates and implements an instance of Array : Array : Collections Data Structure C# Source Code


Custom Search

C# Source Code » Collections Data Structure » Array »

 

Creates and implements an instance of Array









    

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

//
// CreatArr.cs -- Creates and implements an instance of Array
//
//                Compile this program with the following command line:
//                    C:>csc CreatArr.cs
//
namespace nsArray
{
    using System;
    
    public class CreatArr
    {
        static public void Main ()
        {
            DateTime now = DateTime.Now;
            Random rand = new Random ((int) now.Millisecond);

            // Create an instance of the Array class.
            Array Arr = Array.CreateInstance (typeof(Int32), 10);
            
            // Initialize the elements using the SetValue() member method
            // Use the GetLowerBound() and GetUpperBound() methods for safe access.
            for (int x = Arr.GetLowerBound(0); x < Arr.GetUpperBound(0) + 1; ++x)
            {
                Arr.SetValue (rand.Next () % 100, x);
            }
            int Total = 0;
            Console.Write ("Array values are ");
            
            // Use the foreach loop on the Array instance
            foreach (int val in Arr)
            {
                Total += val;
                Console.Write (val + ", ");
            }
            Console.WriteLine ("and the average is {0,0:F1}",
                              (double) Total / (double) Arr.Length);
        }
    }
}



           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Collections Data Structure
» Array