# Arrays

The Array class is a dynamic array. The array is referred to as dynamic because the size of the array is not required to be known in advance. Dynamic arrays may also be sparse in the sense that not all entries are required as for a contiguous array. An example of a program using a dynamic array is shown below.

```// Calculus -- ArrayA -- A simple example of a dynamic array.

using System;
using Calculus;

class Program
{
static void Main()
{
Array<string> Array = new Array<string>();

Array[0] = "C";
Array[1] = "A";
Array[2] = "B";

Console.WriteLine("Original Array == {0}", Array);

Array.Sort();

Console.WriteLine("Sorted Array == {0}", Array);
}
}
```

The declaration of the array is very simple because no dimensions are required. The generic class parameter is all that is required to be known (i.e. the type of the array). For the above program, an array of strings is declared and allocated. Then three strings are placed in the array via the array indexer. The array is then printed.

The array is sorted and printed. The resulting output is shown below.

```Original Array == {C,A,B}
Sorted Array == {A,B,C}
```

The strings have been placed in alphabetic order within the array by the sort.

The next program does some set theory on arrays.

```// Calculus -- ArrayB -- Set Theory on Arrays

using Calculus;
using System;

class Arrays
{
public static void Main()
{
try
{
Array<int> arrayA = new Array<int>(1, 2, 3, 4, 5);
Array<int> arrayB = new Array<int>(3, 4, 5, 6, 7);

Array<int> Union = new Array<int>(arrayA);
Union.UnionWith(arrayB);

Console.WriteLine("{0} | {1} == {2}", arrayA, arrayB, Union);

Array<int> Intersection = new Array<int>(arrayA);
Intersection.IntersectWith(arrayB);

Console.WriteLine("{0} & {1} == {2}", arrayA, arrayB, Intersection);

Array<int> Difference = new Array<int>(arrayA);
Difference.ExceptWith(arrayB);

Console.WriteLine("{0} - {1} == {2}", arrayA, arrayB, Difference);
}
catch (Exception e) { Console.WriteLine("{0}", e); }
}
}
```

The output is shown below.

```{1,2,3,4,5} | {3,4,5,6,7} == {1,2,3,4,5,3,4,5,6,7}
{1,2,3,4,5} & {3,4,5,6,7} == {3,4,5}
{1,2,3,4,5} - {3,4,5,6,7} == {1,2}
```

We see that array union is actually concatenation where duplicates are included and the output is unsorted. Intersection and difference work as expected, but be careful, they are slow on big arrays (Sets and HashSets are better).

The equivalent program for sets is as follows.

```// Calculus -- ArrayC -- Set Theory on Sets

using Calculus;
using System;

class Sets
{
public static void Main()
{
try
{
Set<int> setA = new Set<int>(1, 2, 3, 4, 5);
Set<int> setB = new Set<int>(3, 4, 5, 6, 7);

Set<int> Union = new Set<int>(setA);
Union.UnionWith(setB);

Console.WriteLine("{0} | {1} == {2}", setA, setB, Union);

Set<int> Intersection = new Set<int>(setA);
Intersection.IntersectWith(setB);

Console.WriteLine("{0} & {1} == {2}", setA, setB, Intersection);

Set<int> Difference = new Set<int>(setA);
Difference.ExceptWith(setB);

Console.WriteLine("{0} - {1} == {2}", setA, setB, Difference);
}
catch (Exception e) { Console.WriteLine("{0}", e); }
}
}
```

The output is as follows.

```{1,2,3,4,5} | {3,4,5,6,7} == {1,2,3,4,5,6,7}
{1,2,3,4,5} & {3,4,5,6,7} == {3,4,5}
{1,2,3,4,5} - {3,4,5,6,7} == {1,2}
```

We can see that the extension method UnionWith resolves to the normal meaning of union for sets. The ISet interface defines some basic properties of set theory based upon the Contains and Remove methods of the interface. Union is defined purely on the ICollection interface, it only relies upon the Add method existing.

The following program does the same thing with hash sets.

```// Calculus -- ArrayD -- Set Theory on HashSets

using Calculus;
using System;

class HashSets
{
public static void Main()
{
try
{
HashSet<int> setA = new HashSet<int>(1, 2, 3, 4, 5);
HashSet<int> setB = new HashSet<int>(3, 4, 5, 6, 7);

HashSet<int> Union = new HashSet<int>(setA);
Union.UnionWith(setB);

Console.WriteLine("{0} | {1} == {2}", setA, setB, Union);

HashSet<int> Intersection = new HashSet<int>(setA);
Intersection.IntersectWith(setB);

Console.WriteLine("{0} & {1} == {2}", setA, setB, Intersection);

HashSet<int> Difference = new HashSet<int>(setA);
Difference.ExceptWith(setB);

Console.WriteLine("{0} - {1} == {2}", setA, setB, Difference);
}
catch (Exception e) { Console.WriteLine("{0}", e); }
}
}
```

The output is as follows.

```{1,2,3,4,5} | {3,4,5,6,7} == {1,2,3,4,5,6,7}
{1,2,3,4,5} & {3,4,5,6,7} == {3,4,5}
{1,2,3,4,5} - {3,4,5,6,7} == {1,2}
```