Wednesday, January 18, 2012

Wednesday 1.18.12

class Program
{
static void Main(string[] args)
{
Console.WriteLine("***** Fun with Arrays *****");
SimpleArrays();
ArrayInitialization();
Console.ReadLine();
}

static void SimpleArrays()
{
Console.WriteLine("=> Simple Array Creation.");
//assign an array of ints containing 3 elements {0, 1, 2}
int[] myInts = new int[3];


myInts[0] = 100;
myInts[1] = 200;
myInts[2] = 300;

//now print each value
foreach (int i in myInts)
Console.WriteLine(i);

Console.WriteLine();

//initialize a 100 item string array, indexed {0 - 99}
string[] booksOnDotNet = new string[100];
Console.WriteLine();
}

static void ArrayInitialization()
{
Console.WriteLine("=> Array Initialization.");
//array initialization syntax using the new keyword
string[] stringArray = new string[] { "one", "two", "three" };
Console.WriteLine("stringArray has {0} elements", stringArray.Length);

//array initialization sytnax without using the new keyword
bool[] boolArray = { false, false, true };
Console.WriteLine("boolArray has {0} elements", boolArray.Length);

//array intialization with new keyword and size
int[] intArray = new int[4] { 20, 22, 23, 0 };
Console.WriteLine("intArry has {0} elements", intArray.Length);
Console.WriteLine();

}
}

No comments:

Post a Comment