Arrays in C#

✓ Arrays
An array is an unordered sequence of items, and all the items in an array have the same type. The items in an array live in a contiguous block of memory and are accessed by using an index.

You are not restricted to primitive types as array elements. You can also create arrays of structures, enumerations, and classes. Arrays are reference types and when you declare an array variable, you do not declare its size and no memory is allocated. The array is given memory only when the instance is created.

When you create an array instance, all the elements of the array are initialized to a default value depending on their type(Numeric =0, object = null).

pins = new int[4]; // create an arry with deafault values

int[] pins = new int[4]{ 9, 3, 7, 2 };
int[] pins = { 9, 3, 7, 2 };

Time[] schedule = { new Time(12,30), new Time(5,30) }; // create an array of structures or objects

var names = new[]{"John", "Diana", "James", "Francesca"}; //creating an implicitly typed array

✓ Interating through arrays

int[] pins = { 9, 3, 7, 2 };
for (int index = 0; index < pins.Length; index++)
{
    int pin = pins[index];
    Console.WriteLine(pin);
}

foreach (int pin in pins)
{
    Console.WriteLine(pin);
}

var names = new[] { new { Name = "John", Age = 47 }, new { Name = "Diana", Age = 46 }, new { Name = "James", Age = 20 }, new { Name = "Francesca", Age = 18 } };

foreach (var familyMember in names)
{
    Console.WriteLine("Name: {0}, Age: {1}", familyMember.Name, familyMember.Age);
}

✓ Passing array as parameters

// passing array as parameter
public void ProcessData(int[] data)
{
    foreach (int i in data)
    {
        ...
    }
}

//returning as array
public int[] ReadData()
{
    Console.WriteLine("How many elements?");
    string reply = Console.ReadLine();
    int numElements = int.Parse(reply);
    int[] data = new int[numElements];
    for (int i = 0; i < numElements; i++)
    {
        Console.WriteLine("Enter data for element {0}", i);
        reply = Console.ReadLine();
        int elementData = int.Parse(reply);
        data[i] = elementData;
    }
    return data;
}

✓ Multidimensional arrays

int[,] items = new int[4, 6]; //two dimensional array
int[, ,] cube = new int[5, 5, 5]; // three dimensional array

✓ Jagged arrays – In jagged array, each column has a different length

int[][] items = new int[4][];
int[] columnForRow0 = new int[3];
int[] columnForRow1 = new int[10];
int[] columnForRow2 = new int[40];
int[] columnForRow3 = new int[25];
items[0] = columnForRow0;
items[1] = columnForRow1;
items[2] = columnForRow2;
items[3] = columnForRow3;

✓ A params Array

// method to determine the minimum value in a set of values passed as parametersclass Util
{
    public static int Min(int[] paramList)
    {
        if (paramList == null || paramList.Length == 0)
        {
            throw new ArgumentException("Util.Min: not enough arguments");
        }
        int currentMin = paramList[0];
        foreach (int i in paramList)
        {
             if (i < currentMin)
             {
                 currentMin = i;
             }
         }
        return currentMin;
    }
}

// Using min method
int[] array = new int[3];
array[0] = first;
array[1] = second;
array[2] = third;
int min = Util.Min(array);

int min = Util.Min(new int[] {first, second, third});

✓ Declaring a params Array

class Util
{
    public static int Min(params int[] paramList)
    {
        // code exactly as before
    }
}

// Using min method
int min = Util.Min(first, second, third);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s