Creating and Using Arrays - Example

This section provides a tutorial example on how to create and use an array.

The following program is a good illustration of the different array syntaxes:

// Arrays.cs
// Copyright (c) 2015, HerongYang.com, All Rights Reserved.

class Arrays {
   static void Main() {
      int[] primes;
      primes = new int[10];
      primes[0] = 2;
      primes[2] = 5;
      System.Console.WriteLine("The 3rd prime number is: {0}", 
         primes[2]);
      double[] temperatures = new double[3];
      temperatures[0] = 70.0;
      temperatures[1] = 75.0;
      temperatures[2] = 73.5;
      System.Console.WriteLine("Forecast for tomorrow's high"
         + " temperature: {0}", temperatures[2]);
   }
}

Output:

The 3rd prime number is: 5
Forecast for tomorrow's high temperature: 73.5

Please note that:

Last update: 2015.

Table of Contents

 About This Book

 Introduction of C# (C Sharp)

 Data Type and Variables

 Logical Expressions and Conditional Statements

Arrays and Loop Statements

 Creating and Using Arrays

Creating and Using Arrays - Example

 "for" Loop Statements

 "while" Loop Statements

 Data Type Features

 Floating-Point Data Types

 Passing Parameters to Methods

 Execution Environment Class

 Visual C# 2010 Express Edition

 Class Features

 C# Compiler and Intermediate Language

 Compiling C# Source Code Files

 MSBuild - Microsoft Build Engine

 Memory Usages of Processes

 Multithreading in C#

 Async Feature from C# 5

 System.IO.FileInfo Class

 System.Diagnostics.FileVersionInfo Class

 WPF - Windows Presentation Foundation

 Partial Classes and Partial Methods

 Outdated Tutorials

 References

 PDF Printing Version