Bits and Bytes

Learn to Live

Arrays in C# — February 18, 2011

Arrays in C#

Here is a simple program demonstrating the various Single Dimensional Arrays in C#

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication3

{

class SingleDimensionalArrays

{

static void Main(string[] args)

{

Console.WriteLine(“Arrays in C# \n“);

Console.WriteLine(“Array a”);

int[] a = new int[3]; //Declaring the Array

//Initializing the Array

a[0] = 3;

a[1] = 2;

a[2] = 7;

for (int i = 0; i < 3; i++)

{

Console.Write(a[i] + ” “);

}

Console.WriteLine(“\nLength of Array a is ” + a.Length);

Console.WriteLine(“\n”);

Console.WriteLine(“Array b”);

int[] b = new int[] { 3, 4, 5, 6 }; //Declaring and initializing         the array at the same place

for (int i = 0; i < b.Length; i++)

{

Console.Write(b[i] + ” “);

}

Console.WriteLine(“\n”);

Console.WriteLine(“Array c”);

int[] c = { 1, 2, 3, 4 };

for (int i = 0; i < c.Length; i++)

{

Console.Write(c[i] + ” “);

}

Console.ReadKey();

}

}

}

ArraysC#

Advertisements