03.01.2015 Views

Complete set: Intro to C - Bill Buchanan

Complete set: Intro to C - Bill Buchanan

Complete set: Intro to C - Bill Buchanan

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Note that the declaration of the array type does not actually create the array, and the<br />

line:<br />

double[] inputValues;<br />

double av<br />

av=inputValues[0];<br />

will cause a build error of:<br />

Use of unassigned local variable 'inputValues'<br />

As the array can not been created yet, and just contains a null value.<br />

3.2.1 Initializing arrays<br />

The arrays when they are initialized are <strong>set</strong> <strong>to</strong> a default value, such as zero for numeric<br />

data types, and null for strings. To initialise it with actual values, the required<br />

values are contained within curly brackets, such as:<br />

int i[] = {10,20,30,40,50}; // First element i[0], Last element i[4]<br />

double[] vals = {1.2,1.4,1.6,1.8,2.0};<br />

string[] menuItems = {"File", "Edit", "View", "Insert", "Tool", "Table"};<br />

This performs both the declaration of the array and its initialization. Program 3.2<br />

shows an example of the declaration of an array of strings.<br />

Program 3.2: Program3_2_ArrayInitializationStrings<br />

using System;<br />

duction <strong>to</strong> .NET<br />

<strong>Intro</strong><br />

namespace ConsoleApplication2<br />

{<br />

class ArrayExample01<br />

{<br />

static void Main(string[] args)<br />

{<br />

int i;<br />

}<br />

}<br />

string[] ColourBands={"BLACK","BROWN","RED","ORANGE","YELLOW","GREEN"};<br />

for (i=0;i

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!