Routine List-Based Operations on Arrays

Sorting an Array

When accessing the members of an array, you may want them to be arranged in alphabetical, numerical, or chronological order. To assist you with re-arranging the elements in an array, the Array class is equipped with a method named Sort that is overloaded with as many versions as you can possibly need.

To arrange an array, you can call the following version of the Array.Sort() method:

`public static void Sort(Array array);`

This is a static method that takes as argument the name of the array you want to re-arrange. If your array is a list of primitive values, the sorting operation is automatic. Here is an example:

```using System;

public class Exercise
{
public static void Main()
{
double[] numbers = new double[8]
{
7628.937, 6.48, 574.9, 293749.064,
0.70257, 314.905, 80458.01, 28.07
};

Console.WriteLine("Numbers");
Console.WriteLine("Original List");

foreach (var number in numbers)
Console.WriteLine("Number: " + number);
Console.WriteLine("-----------------------");

Array.Sort(numbers);

Console.WriteLine("Sorted List");
foreach (var number in numbers)
Console.WriteLine("Number: " + number);

Console.WriteLine("=================================");
}
}```

This would produce:

```Numbers
Original List
Number: 7628.937
Number: 6.48
Number: 574.9
Number: 293749.064
Number: 0.70257
Number: 314.905
Number: 80458.01
Number: 28.07
-----------------------
Sorted List
Number: 0.70257
Number: 6.48
Number: 28.07
Number: 314.905
Number: 574.9
Number: 7628.937
Number: 80458.01
Number: 293749.064
=================================
Press any key to continue . . .```

Notice that the numbers are arranged in in ascending order. In the same way, if the array is made of strings, you can call the Array.Sort() method to arrange the list in alphabetical order. If the array is made of dates, you can arrange them in chronological order.

If your array is a list of objects, the class that constitutes the array must implement the IComparable interface that we introduced already in Lesson 30.

Practical Learning: Sorting an Array of Objects

1. Start Microsoft Visual Studio
2. On the main menu, click File -> New -> Project...
3. In the middle list, click Console App (.NET Framework)
4. Change the Name of the project to CountriesStatistics3
5. Click OK
6. In the Solution Explorer, right-click CountriesStatistics3 -> Add -> Class...
7. Type Region as the Name of the class
9. Change the document as follows:
```namespace CountriesStatistics3
{
public class Region
{
public string Designation { get; set; }
public string Description { get; set; }
}
}```
10. In the Solution Explorer, right-click CountriesStatistics3 -> Add -> New Item...
11. In the left frame of the Add New Item dialog box, click Code and, in the middle frame, click Interface
12. Change the Name to Abbreviated
14. Create a string-based property as follows:
```namespace CountriesStatistics3
{
interface IAbbreviated
{
string Abbreviation { get; set; }
}
}
```
15. In the Solution Explorer, right-click CountriesStatistics3 -> Add -> New Item
16. In the middle list of the dialog box, make sure Interface is selectd. Change the Name to GovernmentEntity
18. Change the class as follows:
```namespace CountriesStatistics3
{
interface IGovernmentEntity
{
string Name    { get; set; }
int    Area    { get;      }
string Capital { get; set; }
}
}```
19. In the Solution Explorer, right-click CountriesStatistics3 -> Add -> Class...
20. Type State as the name of the file
21. Press Enter
22. Change the class as follows:
```using System;

namespace CountriesStatistics3
{
public class State : IAbbreviated,
IGovernmentEntity,
IComparable
{
// From the IAbbreviated interface
public string Abbreviation { get; set; }
// From the IGovernmentEntity interface
public string Name    { get; set; }
public int Area       { get; set; }
public string Capital { get; set; }

// New Properties
public string StateName => Name;
public int AreaSqrMiles => Area;
public int AreaSqrKms   { get; set; }
public Region Region    { get; set; }

public int CompareTo(object stt)
{
if (stt == null)
{
return 0;
}

State other = stt as State;

if (other != null)
return StateName.CompareTo(other.StateName);

return 0;
}
}
}```
23. In the Solution Explorer, right-click CountriesStatistics3 -> Add -> Class...
24. Type Federation
26. Change the class as follows:
```namespace CountriesStatistics2
{
public class Federation : State
{
public string AdmissionUnionDate  { get; set; }
public int      AdmissionUnionOrder { get; set; }
}
}```
27. In the Solution Explorer, right-click Program.cs and click Rename
28. Type CountriesStatistics to get CountriesStatistics.cs
29. Read the message box and click Yes
30. In the Solution Explorer, double-click CountriesStatistics.cs to access it
31. Change the code as follows:
```using System;

namespace CountriesStatistics3
{
public class CountriesStatistics
{
private static void Display(Array entities)
{
Console.Clear();

Console.WriteLine(" # Abbrv State Name        SqrMls     Km2    Date        Order  Capital         Region");
Console.WriteLine("----------------------------------------------------------------------------------------------------");

for (int counter = 0; counter <= entities.Length - 1; counter++)
{
Federation stt = (Federation)entities.GetValue(counter);

Console.WriteLine("{0,2}  {1,2}   {2}   {3,6}   {4,7}   {5,8} {6,5}   {7}  {8}",
counter + 1, stt.Abbreviation, stt.StateName,
}
Console.WriteLine("====================================================================================================");
}

public static int Main(string[] args)
{
Array states = Array.CreateInstance(typeof(Federation), 30);
Array regions = Array.CreateInstance(typeof(Region), 9);

regions.SetValue(new Region() { Designation = "East North Central", Description = "The East North Central region includes the states around the Great Lakes." }, 0);
regions.SetValue(new Region() { Designation = "East South Central", Description = "The East South Central portion is one of the regions designated as the South." }, 1);
regions.SetValue(new Region() { Designation = "New England", Description = "New England is the group of states in the North-East region. It is delimited in the North and North-East by Canada, in the East by the Atlantic Ocean, and in the South and West by the New York state." }, 2);
regions.SetValue(new Region() { Designation = "Mid-Atlantic", Description = "Mid-Atlantic is a region situated in the south of New England. Mid-Atlantic is one of the regions defined by the Census bureau for statistical purposes." }, 3);
regions.SetValue(new Region() { Designation = "Mountain", Description = "Like the name suggests, the Mountain region covers states known for their mountaneous characteristics. They are also covered by desertic areas." }, 4);
regions.SetValue(new Region() { Designation = "Pacific", Description = "The Pacific region covers the costal western states plus the two non-continental states of Alaska and the Hawaiian islands. All states in this region have a coast on the Pacific Ocean." }, 5);
regions.SetValue(new Region() { Designation = "South Atlantic", Description = "The South Atlantic region includes the states in the South-East part but also counts the Disctrict of Columbia." }, 6);
regions.SetValue(new Region() { Designation = "West North Central", Description = "The West North Central region includes the states in the Great Planes area. This reqion is divided from the East North Central part by the Mississippi River. This region is characterized by vast agricultural farms and high employment." }, 7);
regions.SetValue(new Region() { Designation = "West South Central", Description = "The West South Central part is one of the regions with (only) four states. The imposing Texas state is both the largest and the most populous state in the region." }, 8);

states.SetValue(new Federation() { Abbreviation = "RI", Name = "Rhode Island  ", Area = 1545, AreaSqrKms = 4002, AdmissionUnionDate = "05/29/1790", AdmissionUnionOrder = 13, Capital = "Providence    ", Region = (Region)(regions.GetValue(2)) }, 0);
states.SetValue(new Federation() { Abbreviation = "OH", Name = "Ohio          ", Area = 44828, AreaSqrKms = 116103, AdmissionUnionDate = "03/01/1803", AdmissionUnionOrder = 17, Capital = "Columbus      ", Region = (Region)(regions.GetValue(0)) }, 1);
states.SetValue(new Federation() { Abbreviation = "KY", Name = "Kentucky      ", Area = 40411, AreaSqrKms = 104665, AdmissionUnionDate = "06/01/1792", AdmissionUnionOrder = 15, Capital = "Frankfort     ", Region = (Region)(regions.GetValue(1)) }, 2);
states.SetValue(new Federation() { Abbreviation = "IA", Name = "Iowa          ", Area = 56276, AreaSqrKms = 145754, AdmissionUnionDate = "12/28/1846", AdmissionUnionOrder = 29, Capital = "Des Moines    ", Region = (Region)(regions.GetValue(7)) }, 3);
states.SetValue(new Federation() { Abbreviation = "WI", Name = "Wisconsin     ", Area = 65503, AreaSqrKms = 169653, AdmissionUnionDate = "05/29/1848", AdmissionUnionOrder = 30, Capital = "Madison       ", Region = (Region)(regions.GetValue(0)) }, 4);
states.SetValue(new Federation() { Abbreviation = "VT", Name = "Vermont       ", Area = 9615, AreaSqrKms = 24903, AdmissionUnionDate = "03/04/1791", AdmissionUnionOrder = 14, Capital = "Montpelier    ", Region = (Region)(regions.GetValue(2)) }, 5);
states.SetValue(new Federation() { Abbreviation = "ID", Name = "Idaho         ", Area = 83574, AreaSqrKms = 216456, AdmissionUnionDate = "07/03/1890", AdmissionUnionOrder = 43, Capital = "Boise         ", Region = (Region)(regions.GetValue(4)) }, 6);
states.SetValue(new Federation() { Abbreviation = "ME", Name = "Maine         ", Area = 35387, AreaSqrKms = 91653, AdmissionUnionDate = "03/15/1820", AdmissionUnionOrder = 23, Capital = "Augusta       ", Region = (Region)(regions.GetValue(2)) }, 7);
states.SetValue(new Federation() { Abbreviation = "OR", Name = "Oregon        ", Area = 98386, AreaSqrKms = 254819, AdmissionUnionDate = "02/14/1859", AdmissionUnionOrder = 33, Capital = "Salem         ", Region = (Region)(regions.GetValue(5)) }, 8);
states.SetValue(new Federation() { Abbreviation = "ND", Name = "North Dakota  ", Area = 70704, AreaSqrKms = 183123, AdmissionUnionDate = "11/02/1889", AdmissionUnionOrder = 39, Capital = "Bismarck      ", Region = (Region)(regions.GetValue(7)) }, 9);
states.SetValue(new Federation() { Abbreviation = "IN", Name = "Indiana       ", Area = 36420, AreaSqrKms = 94328, AdmissionUnionDate = "12/11/1816", AdmissionUnionOrder = 19, Capital = "Indianapolis  ", Region = (Region)(regions.GetValue(0)) }, 10);
states.SetValue(new Federation() { Abbreviation = "MS", Name = "Mississippi   ", Area = 48434, AreaSqrKms = 125443, AdmissionUnionDate = "12/10/1817", AdmissionUnionOrder = 20, Capital = "Jackson       ", Region = (Region)(regions.GetValue(1)) }, 11);
states.SetValue(new Federation() { Abbreviation = "TX", Name = "Texas         ", Area = 268601, AreaSqrKms = 695676, AdmissionUnionDate = "12/29/1845", AdmissionUnionOrder = 28, Capital = "Austin        ", Region = (Region)(regions.GetValue(8)) }, 12);
states.SetValue(new Federation() { Abbreviation = "MT", Name = "Montana       ", Area = 147046, AreaSqrKms = 380850, AdmissionUnionDate = "11/08/1889", AdmissionUnionOrder = 41, Capital = "Helena        ", Region = (Region)(regions.GetValue(4)) }, 13);
states.SetValue(new Federation() { Abbreviation = "NC", Name = "North Carolina", Area = 53821, AreaSqrKms = 139397, AdmissionUnionDate = "11/21/1789", AdmissionUnionOrder = 12, Capital = "Raleigh       ", Region = (Region)(regions.GetValue(6)) }, 14);
states.SetValue(new Federation() { Abbreviation = "TN", Name = "Tennessee     ", Area = 42146, AreaSqrKms = 109158, AdmissionUnionDate = "06/01/1796", AdmissionUnionOrder = 16, Capital = "Nashville     ", Region = (Region)(regions.GetValue(1)) }, 15);
states.SetValue(new Federation() { Abbreviation = "NE", Name = "Nebraska      ", Area = 77358, AreaSqrKms = 200358, AdmissionUnionDate = "03/01/1867", AdmissionUnionOrder = 37, Capital = "Lincoln       ", Region = (Region)(regions.GetValue(7)) }, 16);
states.SetValue(new Federation() { Abbreviation = "IL", Name = "Illinois      ", Area = 57918, AreaSqrKms = 150007, AdmissionUnionDate = "12/03/1818", AdmissionUnionOrder = 21, Capital = "Springfield   ", Region = (Region)(regions.GetValue(0)) }, 17);
states.SetValue(new Federation() { Abbreviation = "KS", Name = "Kansas        ", Area = 82282, AreaSqrKms = 213110, AdmissionUnionDate = "01/29/1861", AdmissionUnionOrder = 34, Capital = "Topeka        ", Region = (Region)(regions.GetValue(7)) }, 18);
states.SetValue(new Federation() { Abbreviation = "NH", Name = "New Hampshire ", Area = 9351, AreaSqrKms = 24219, AdmissionUnionDate = "06/21/1788", AdmissionUnionOrder = 9, Capital = "Concord       ", Region = (Region)(regions.GetValue(2)) }, 19);
states.SetValue(new Federation() { Abbreviation = "DE", Name = "Delaware      ", Area = 2489, AreaSqrKms = 6447, AdmissionUnionDate = "12/07/1787", AdmissionUnionOrder = 1, Capital = "Dover         ", Region = (Region)(regions.GetValue(6)) }, 20);
states.SetValue(new Federation() { Abbreviation = "NJ", Name = "New Jersey    ", Area = 8722, AreaSqrKms = 22590, AdmissionUnionDate = "12/18/1787", AdmissionUnionOrder = 3, Capital = "Trenton       ", Region = (Region)(regions.GetValue(3)) }, 21);
states.SetValue(new Federation() { Abbreviation = "AK", Name = "Alaska        ", Area = 656424, AreaSqrKms = 1700139, AdmissionUnionDate = "01/03/1959", AdmissionUnionOrder = 49, Capital = "Juneau        ", Region = (Region)(regions.GetValue(5)) }, 22);
states.SetValue(new Federation() { Abbreviation = "NM", Name = "New Mexico    ", Area = 121598, AreaSqrKms = 314939, AdmissionUnionDate = "01/06/1912", AdmissionUnionOrder = 47, Capital = "Santa Fe      ", Region = (Region)(regions.GetValue(4)) }, 23);
states.SetValue(new Federation() { Abbreviation = "NY", Name = "New York      ", Area = 54475, AreaSqrKms = 141089, AdmissionUnionDate = "07/26/1788", AdmissionUnionOrder = 11, Capital = "Albany        ", Region = (Region)(regions.GetValue(3)) }, 24);
states.SetValue(new Federation() { Abbreviation = "CA", Name = "California    ", Area = 163707, AreaSqrKms = 424002, AdmissionUnionDate = "09/09/1850", AdmissionUnionOrder = 31, Capital = "Sacramento    ", Region = (Region)(regions.GetValue(5)) }, 25);
states.SetValue(new Federation() { Abbreviation = "MO", Name = "Missouri      ", Area = 69709, AreaSqrKms = 180546, AdmissionUnionDate = "08/10/1821", AdmissionUnionOrder = 24, Capital = "Jefferson City", Region = (Region)(regions.GetValue(7)) }, 26);
states.SetValue(new Federation() { Abbreviation = "OK", Name = "Oklahoma      ", Area = 69903, AreaSqrKms = 181049, AdmissionUnionDate = "11/16/1907", AdmissionUnionOrder = 46, Capital = "Oklahoma City ", Region = (Region)(regions.GetValue(8)) }, 27);
states.SetValue(new Federation() { Abbreviation = "PA", Name = "Pennsylvania  ", Area = 46058, AreaSqrKms = 119291, AdmissionUnionDate = "12/12/1787", AdmissionUnionOrder = 2, Capital = "Harrisburg    ", Region = (Region)(regions.GetValue(3)) }, 28);
states.SetValue(new Federation() { Abbreviation = "SC", Name = "South Carolina", Area = 32007, AreaSqrKms = 82898, AdmissionUnionDate = "05/23/1788", AdmissionUnionOrder = 8, Capital = "Columbia      ", Region = (Region)(regions.GetValue(6)) }, 29);

Display(states);

Console.Write("Do you want to sort the list in alphabetical order (y/n)? ");

{
Array.Sort(states);
Display(states);
}
else
{
Console.WriteLine("Good Bye!!!");
Console.WriteLine("====================================================================================================");
}

return 1;
}
}
}```
```                                 Area      Admission to Union
# Abbrv State Name        SqrMls     Km2    Date        Order  Capital         Region
----------------------------------------------------------------------------------------------------
1  RI   Rhode Island       1545      4002   05/29/1790    13   Providence      New England
2  OH   Ohio              44828    116103   03/01/1803    17   Columbus        East North Central
3  KY   Kentucky          40411    104665   06/01/1792    15   Frankfort       East South Central
4  IA   Iowa              56276    145754   12/28/1846    29   Des Moines      West North Central
5  WI   Wisconsin         65503    169653   05/29/1848    30   Madison         East North Central
6  VT   Vermont            9615     24903   03/04/1791    14   Montpelier      New England
7  ID   Idaho             83574    216456   07/03/1890    43   Boise           Mountain
8  ME   Maine             35387     91653   03/15/1820    23   Augusta         New England
9  OR   Oregon            98386    254819   02/14/1859    33   Salem           Pacific
10  ND   North Dakota      70704    183123   11/02/1889    39   Bismarck        West North Central
11  IN   Indiana           36420     94328   12/11/1816    19   Indianapolis    East North Central
12  MS   Mississippi       48434    125443   12/10/1817    20   Jackson         East South Central
13  TX   Texas            268601    695676   12/29/1845    28   Austin          West South Central
14  MT   Montana          147046    380850   11/08/1889    41   Helena          Mountain
15  NC   North Carolina    53821    139397   11/21/1789    12   Raleigh         South Atlantic
16  TN   Tennessee         42146    109158   06/01/1796    16   Nashville       East South Central
17  NE   Nebraska          77358    200358   03/01/1867    37   Lincoln         West North Central
18  IL   Illinois          57918    150007   12/03/1818    21   Springfield     East North Central
19  KS   Kansas            82282    213110   01/29/1861    34   Topeka          West North Central
20  NH   New Hampshire      9351     24219   06/21/1788     9   Concord         New England
21  DE   Delaware           2489      6447   12/07/1787     1   Dover           South Atlantic
22  NJ   New Jersey         8722     22590   12/18/1787     3   Trenton         Mid-Atlantic
23  AK   Alaska           656424   1700139   01/03/1959    49   Juneau          Pacific
24  NM   New Mexico       121598    314939   01/06/1912    47   Santa Fe        Mountain
25  NY   New York          54475    141089   07/26/1788    11   Albany          Mid-Atlantic
26  CA   California       163707    424002   09/09/1850    31   Sacramento      Pacific
27  MO   Missouri          69709    180546   08/10/1821    24   Jefferson City  West North Central
28  OK   Oklahoma          69903    181049   11/16/1907    46   Oklahoma City   West South Central
29  PA   Pennsylvania      46058    119291   12/12/1787     2   Harrisburg      Mid-Atlantic
30  SC   South Carolina    32007     82898   05/23/1788     8   Columbia        South Atlantic
====================================================================================================
Do you want to sort the list in alphabetical order (y/n)?```
33. When prompted, type y and press Enter:
```                                 Area      Admission to Union
# Abbrv State Name        SqrMls     Km2    Date        Order  Capital         Region
----------------------------------------------------------------------------------------------------
1  AK   Alaska           656424   1700139   01/03/1959    49   Juneau          Pacific
2  CA   California       163707    424002   09/09/1850    31   Sacramento      Pacific
3  DE   Delaware           2489      6447   12/07/1787     1   Dover           South Atlantic
4  ID   Idaho             83574    216456   07/03/1890    43   Boise           Mountain
5  IL   Illinois          57918    150007   12/03/1818    21   Springfield     East North Central
6  IN   Indiana           36420     94328   12/11/1816    19   Indianapolis    East North Central
7  IA   Iowa              56276    145754   12/28/1846    29   Des Moines      West North Central
8  KS   Kansas            82282    213110   01/29/1861    34   Topeka          West North Central
9  KY   Kentucky          40411    104665   06/01/1792    15   Frankfort       East South Central
10  ME   Maine             35387     91653   03/15/1820    23   Augusta         New England
11  MS   Mississippi       48434    125443   12/10/1817    20   Jackson         East South Central
12  MO   Missouri          69709    180546   08/10/1821    24   Jefferson City  West North Central
13  MT   Montana          147046    380850   11/08/1889    41   Helena          Mountain
14  NE   Nebraska          77358    200358   03/01/1867    37   Lincoln         West North Central
15  NH   New Hampshire      9351     24219   06/21/1788     9   Concord         New England
16  NJ   New Jersey         8722     22590   12/18/1787     3   Trenton         Mid-Atlantic
17  NM   New Mexico       121598    314939   01/06/1912    47   Santa Fe        Mountain
18  NY   New York          54475    141089   07/26/1788    11   Albany          Mid-Atlantic
19  NC   North Carolina    53821    139397   11/21/1789    12   Raleigh         South Atlantic
20  ND   North Dakota      70704    183123   11/02/1889    39   Bismarck        West North Central
21  OH   Ohio              44828    116103   03/01/1803    17   Columbus        East North Central
22  OK   Oklahoma          69903    181049   11/16/1907    46   Oklahoma City   West South Central
23  OR   Oregon            98386    254819   02/14/1859    33   Salem           Pacific
24  PA   Pennsylvania      46058    119291   12/12/1787     2   Harrisburg      Mid-Atlantic
25  RI   Rhode Island       1545      4002   05/29/1790    13   Providence      New England
26  SC   South Carolina    32007     82898   05/23/1788     8   Columbia        South Atlantic
27  TN   Tennessee         42146    109158   06/01/1796    16   Nashville       East South Central
28  TX   Texas            268601    695676   12/29/1845    28   Austin          West South Central
29  VT   Vermont            9615     24903   03/04/1791    14   Montpelier      New England
30  WI   Wisconsin         65503    169653   05/29/1848    30   Madison         East North Central
====================================================================================================
Press any key to continue . . .```

Reversing an Array

When you display the values of an array, they appear in the order they were added in the list. If you want, you can display the list from the last added item to the first added. To assist you with this operation, the Array class provides a method named Reverse. Its syntax is:

`public static void Reverse(Array array);`

This method neither arranges an array nor reverses a sorted array, It simply reverses whatever order the array holds. Here is an example of calling this method:

```using System;

public class Exercise
{
public static void Main()
{
double[] numbers = new double[8]
{
7628.937, 6.48, 574.9, 293749.064,
0.70257, 314.905, 80458.01, 28.07
};

Console.WriteLine("Numbers");
Console.WriteLine("Original List");

foreach (var number in numbers)
Console.WriteLine("Number: " + number);
Console.WriteLine("-----------------------");

Array.Reverse(numbers);

Console.WriteLine("Sorted List");
foreach (var number in numbers)
Console.WriteLine("Number: " + number);

Console.WriteLine("=================================");
}
}```

This would produce:

```Numbers
Original List
Number: 7628.937
Number: 6.48
Number: 574.9
Number: 293749.064
Number: 0.70257
Number: 314.905
Number: 80458.01
Number: 28.07
-----------------------
Sorted List
Number: 28.07
Number: 80458.01
Number: 314.905
Number: 0.70257
Number: 293749.064
Number: 574.9
Number: 6.48
Number: 7628.937
=================================
Press any key to continue . . .```

Resizing an Array

By definition, an array is is fixed list. This means that, when creating an array, you specify the number of its elements, then you starting adding values or objects to the array, but you cannot add items beyond the specified number. Sometimes in the real world, you may want to add items to array without having to recreate the array. To assist you with this operation, the Array class includes a static method named Resize.

The Array.Resize() method takes two arguments. The first argument is passed by reference and is the name of the array you want to resize. The second argument is a constant integer that is the new size of the array. If the new size is lower than the length of the original array, a new array is created with that new size. This means that, to increase the size of an array, you should pass the second argument as the size of the orginal array + the number of new items you want to add.

Practical Learning: Resizing an Array

1. Change the code as follows:
```using System;

namespace CountriesStatistics3
{
public class CountriesStatistics
{
private static void Display(Array entities)
{
Console.Clear();

Console.WriteLine("                                 United States of America");
Console.WriteLine("====================================================================================================");
Console.WriteLine(" # Abbrv State Name        SqrMls     Km2    Date        Order  Capital         Region");
Console.WriteLine("----------------------------------------------------------------------------------------------------");

for (int counter = 0; counter <= entities.Length - 1; counter++)
{
Federation stt = (Federation)entities.GetValue(counter);

Console.WriteLine("{0,2}  {1,2}   {2}   {3,6}   {4,7}   {5,8} {6,5}   {7}  {8}",
counter + 1, stt.Abbreviation, stt.StateName,
}
Console.WriteLine("====================================================================================================");
}

public static int Main(string[] args)
{
int counter = 0;

Region[] regions = new Region[]
{
new Region() { Designation = "East North Central", Description = "The East North Central region includes the states around the Great Lakes." },
new Region() { Designation = "East South Central", Description = "The East South Central portion is one of the regions designated as the South." },
new Region() { Designation = "New England       ", Description = "New England is the group of states in the North-East region. It is delimited in the North and North-East by Canada, in the East by the Atlantic Ocean, and in the South and West by the New York state." },
new Region() { Designation = "Mid-Atlantic      ", Description = "Mid-Atlantic is a region situated in the south of New England. Mid-Atlantic is one of the regions defined by the Census bureau for statistical purposes." },
new Region() { Designation = "Mountain          ", Description = "Like the name suggests, the Mountain region covers states known for their mountaneous characteristics. They are also covered by desertic areas." },
new Region() { Designation = "Pacific           ", Description = "The Pacific region covers the costal western states plus the two non-continental states of Alaska and the Hawaiian islands. All states in this region have a coast on the Pacific Ocean." },
new Region() { Designation = "South Atlantic    ", Description = "The South Atlantic region includes the states in the South-East part but also counts the Disctrict of Columbia." },
new Region() { Designation = "West North Central", Description = "The West North Central region includes the states in the Great Planes area. This reqion is divided from the East North Central part by the Mississippi River. This region is characterized by vast agricultural farms and high employment." },
new Region() { Designation = "West South Central", Description = "The West South Central part is one of the regions with (only) four states. The imposing Texas state is both the largest and the most populous state in the region." }
};

Federation[] states = new Federation[40];

states[0]  = new Federation() { Abbreviation = "RI", Name = "Rhode Island  ", Area =   1545, AreaSqrKms =    4002, AdmissionUnionDate = "05/29/1790", AdmissionUnionOrder = 13, Capital = "Providence    ", Region = regions[2] };
states[1]  = new Federation() { Abbreviation = "OH", Name = "Ohio          ", Area =  44828, AreaSqrKms =  116103, AdmissionUnionDate = "03/01/1803", AdmissionUnionOrder = 17, Capital = "Columbus      ", Region = regions[0] };
states[2]  = new Federation() { Abbreviation = "KY", Name = "Kentucky      ", Area =  40411, AreaSqrKms =  104665, AdmissionUnionDate = "06/01/1792", AdmissionUnionOrder = 15, Capital = "Frankfort     ", Region = regions[2] };
states[3]  = new Federation() { Abbreviation = "IA", Name = "Iowa          ", Area =  56276, AreaSqrKms =  145754, AdmissionUnionDate = "12/28/1846", AdmissionUnionOrder = 29, Capital = "Des Moines    ", Region = regions[7] };
states[4]  = new Federation() { Abbreviation = "WI", Name = "Wisconsin     ", Area =  65503, AreaSqrKms =  169653, AdmissionUnionDate = "05/29/1848", AdmissionUnionOrder = 30, Capital = "Madison       ", Region = regions[0] };
states[5]  = new Federation() { Abbreviation = "VT", Name = "Vermont       ", Area =   9615, AreaSqrKms =   24903, AdmissionUnionDate = "03/04/1791", AdmissionUnionOrder = 14, Capital = "Montpelier    ", Region = regions[2] };
states[6]  = new Federation() { Abbreviation = "ID", Name = "Idaho         ", Area =  83574, AreaSqrKms =  216456, AdmissionUnionDate = "07/03/1890", AdmissionUnionOrder = 43, Capital = "Boise         ", Region = regions[4] };
states[7]  = new Federation() { Abbreviation = "ME", Name = "Maine         ", Area =  35387, AreaSqrKms =   91653, AdmissionUnionDate = "03/15/1820", AdmissionUnionOrder = 23, Capital = "Augusta       ", Region = regions[2] };
states[8]  = new Federation() { Abbreviation = "OR", Name = "Oregon        ", Area =  98386, AreaSqrKms =  254819, AdmissionUnionDate = "02/14/1859", AdmissionUnionOrder = 33, Capital = "Salem         ", Region = regions[5] };
states[9]  = new Federation() { Abbreviation = "ND", Name = "North Dakota  ", Area =  70704, AreaSqrKms =  183123, AdmissionUnionDate = "11/02/1889", AdmissionUnionOrder = 39, Capital = "Bismarck      ", Region = regions[7] };
states[10] = new Federation() { Abbreviation = "IN", Name = "Indiana       ", Area =  36420, AreaSqrKms =   94328, AdmissionUnionDate = "12/11/1816", AdmissionUnionOrder = 19, Capital = "Indianapolis  ", Region = regions[0] };
states[11] = new Federation() { Abbreviation = "MS", Name = "Mississippi   ", Area =  48434, AreaSqrKms =  125443, AdmissionUnionDate = "12/10/1817", AdmissionUnionOrder = 20, Capital = "Jackson       ", Region = regions[1] };
states[12] = new Federation() { Abbreviation = "TX", Name = "Texas         ", Area = 268601, AreaSqrKms =  695676, AdmissionUnionDate = "12/29/1845", AdmissionUnionOrder = 28, Capital = "Austin        ", Region = regions[8] };
states[13] = new Federation() { Abbreviation = "MT", Name = "Montana       ", Area = 147046, AreaSqrKms =  380850, AdmissionUnionDate = "11/08/1889", AdmissionUnionOrder = 41, Capital = "Helena        ", Region = regions[4] };
states[14] = new Federation() { Abbreviation = "NC", Name = "North Carolina", Area =  53821, AreaSqrKms =  139397, AdmissionUnionDate = "11/21/1789", AdmissionUnionOrder = 12, Capital = "Raleigh       ", Region = regions[6] };
states[15] = new Federation() { Abbreviation = "TN", Name = "Tennessee     ", Area =  42146, AreaSqrKms =  109158, AdmissionUnionDate = "06/01/1796", AdmissionUnionOrder = 16, Capital = "Nashville     ", Region = regions[1] };
states[16] = new Federation() { Abbreviation = "NE", Name = "Nebraska      ", Area =  77358, AreaSqrKms =  200358, AdmissionUnionDate = "03/01/1867", AdmissionUnionOrder = 37, Capital = "Lincoln       ", Region = regions[7] };
states[17] = new Federation() { Abbreviation = "IL", Name = "Illinois      ", Area =  57918, AreaSqrKms =  150007, AdmissionUnionDate = "12/03/1818", AdmissionUnionOrder = 21, Capital = "Springfield   ", Region = regions[0] };
states[18] = new Federation() { Abbreviation = "KS", Name = "Kansas        ", Area =  82282, AreaSqrKms =  213110, AdmissionUnionDate = "01/29/1861", AdmissionUnionOrder = 34, Capital = "Topeka        ", Region = regions[7] };
states[19] = new Federation() { Abbreviation = "NH", Name = "New Hampshire ", Area =   9351, AreaSqrKms =   24219, AdmissionUnionDate = "06/21/1788", AdmissionUnionOrder =  9, Capital = "Concord       ", Region = regions[2] };
states[20] = new Federation() { Abbreviation = "DE", Name = "Delaware      ", Area =   2489, AreaSqrKms =    6447, AdmissionUnionDate = "12/07/1787", AdmissionUnionOrder =  1, Capital = "Dover         ", Region = regions[6] };
states[21] = new Federation() { Abbreviation = "NJ", Name = "New Jersey    ", Area =   8722, AreaSqrKms =   22590, AdmissionUnionDate = "12/18/1787", AdmissionUnionOrder =  3, Capital = "Trenton       ", Region = regions[3] };
states[22] = new Federation() { Abbreviation = "AK", Name = "Alaska        ", Area = 656424, AreaSqrKms = 1700139, AdmissionUnionDate = "01/03/1959", AdmissionUnionOrder = 49, Capital = "Juneau        ", Region = regions[5] };
states[23] = new Federation() { Abbreviation = "NM", Name = "New Mexico    ", Area = 121598, AreaSqrKms =  314939, AdmissionUnionDate = "01/06/1912", AdmissionUnionOrder = 47, Capital = "Santa Fe      ", Region = regions[4] };
states[24] = new Federation() { Abbreviation = "NY", Name = "New York      ", Area =  54475, AreaSqrKms =  141089, AdmissionUnionDate = "07/26/1788", AdmissionUnionOrder = 11, Capital = "Albany        ", Region = regions[3] };
states[25] = new Federation() { Abbreviation = "CA", Name = "California    ", Area = 163707, AreaSqrKms =  424002, AdmissionUnionDate = "09/09/1850", AdmissionUnionOrder = 31, Capital = "Sacramento    ", Region = regions[5] };
states[26] = new Federation() { Abbreviation = "MO", Name = "Missouri      ", Area =  69709, AreaSqrKms =  180546, AdmissionUnionDate = "08/10/1821", AdmissionUnionOrder = 24, Capital = "Jefferson City", Region = regions[7] };
states[27] = new Federation() { Abbreviation = "OK", Name = "Oklahoma      ", Area =  69903, AreaSqrKms =  181049, AdmissionUnionDate = "11/16/1907", AdmissionUnionOrder = 46, Capital = "Oklahoma City ", Region = regions[8] };
states[28] = new Federation() { Abbreviation = "PA", Name = "Pennsylvania  ", Area =  46058, AreaSqrKms =  119291, AdmissionUnionDate = "12/12/1787", AdmissionUnionOrder =  2, Capital = "Harrisburg    ", Region = regions[3] };
states[29] = new Federation() { Abbreviation = "SC", Name = "South Carolina", Area =  32007, AreaSqrKms =   82898, AdmissionUnionDate = "05/23/1788", AdmissionUnionOrder =  8, Capital = "Columbia      ", Region = regions[6] };
states[30] = new Federation() { Abbreviation = "WY", Name = "Wyoming       ", Area =  97818, AreaSqrKms =  253349, AdmissionUnionDate = "07/10/1890", AdmissionUnionOrder = 44, Capital = "Cheyenne      ", Region = regions[4] };
states[31] = new Federation() { Abbreviation = "SD", Name = "South Dakota  ", Area =  77122, AreaSqrKms =  199745, AdmissionUnionDate = "11/02/1889", AdmissionUnionOrder = 40, Capital = "Pierre        ", Region = regions[7] };
states[32] = new Federation() { Abbreviation = "UT", Name = "Utah          ", Area =  84904, AreaSqrKms =  219902, AdmissionUnionDate = "01/04/1896", AdmissionUnionOrder = 45, Capital = "Salt Lake City", Region = regions[4] };
states[33] = new Federation() { Abbreviation = "AL", Name = "Alabama       ", Area =  52423, AreaSqrKms =  135775, AdmissionUnionDate = "12/14/1819", AdmissionUnionOrder = 22, Capital = "Montgomery    ", Region = regions[1] };
states[34] = new Federation() { Abbreviation = "VT", Name = "Vermont       ", Area =   9615, AreaSqrKms =   24903, AdmissionUnionDate = "03/04/1791", AdmissionUnionOrder = 14, Capital = "Montpelier    ", Region = regions[2] };
states[35] = new Federation() { Abbreviation = "AR", Name = "Arkansas      ", Area =  53182, AreaSqrKms =  137742, AdmissionUnionDate = "06/15/1836", AdmissionUnionOrder = 25, Capital = "Little Rock   ", Region = regions[8] };
states[36] = new Federation() { Abbreviation = "WA", Name = "Washington    ", Area =  71303, AreaSqrKms =  184674, AdmissionUnionDate = "11/11/1889", AdmissionUnionOrder = 42, Capital = "Olympia       ", Region = regions[5] };
states[37] = new Federation() { Abbreviation = "AZ", Name = "Arizona       ", Area = 114006, AreaSqrKms =  295276, AdmissionUnionDate = "02/14/1912", AdmissionUnionOrder = 48, Capital = "Phoenix       ", Region = regions[4] };
states[38] = new Federation() { Abbreviation = "WV", Name = "West Virginia ", Area =  24231, AreaSqrKms =   62759, AdmissionUnionDate = "06/20/1863", AdmissionUnionOrder = 35, Capital = "Charleston    ", Region = regions[6] };
states[39] = new Federation() { Abbreviation = "LA", Name = "Louisiana     ", Area =  51844, AreaSqrKms =  134275, AdmissionUnionDate = "04/30/1812", AdmissionUnionOrder = 18, Capital = "Baton Rouge   ", Region = regions[8] };

Array.Resize(ref states, states.Length + 5);

states[40] = new Federation() { Abbreviation = "MA", Name = "Massachusetts ", Area =  10555, AreaSqrKms =   27337, AdmissionUnionDate = "02/06/1788", AdmissionUnionOrder =  6, Capital = "Boston        ", Region = regions[2] };
states[41] = new Federation() { Abbreviation = "FL", Name = "Florida       ", Area =  65758, AreaSqrKms =  170313, AdmissionUnionDate = "03/03/1845", AdmissionUnionOrder = 27, Capital = "Tallahassee   ", Region = regions[6] };
states[42] = new Federation() { Abbreviation = "GA", Name = "Georgia       ", Area =  59441, AreaSqrKms =  153953, AdmissionUnionDate = "01/02/1788", AdmissionUnionOrder =  4, Capital = "Atlanta       ", Region = regions[6] };
states[43] = new Federation() { Abbreviation = "HI", Name = "Hawaii        ", Area =  10932, AreaSqrKms =   28313, AdmissionUnionDate = "08/21/1959", AdmissionUnionOrder = 50, Capital = "Honolulu      ", Region = regions[5] };
states[44] = new Federation() { Abbreviation = "MD", Name = "Maryland      ", Area =  12407, AreaSqrKms =   32135, AdmissionUnionDate = "04/28/1788", AdmissionUnionOrder =  7, Capital = "Annapolis     ", Region = regions[6] };

Display(states);

Console.Write("Do you want to sort the list in alphabetical order (y/n)? ");

{
Array.Sort(states);
Display(states);
}
else
{
Console.WriteLine("Good Bye!!!");
Console.WriteLine("====================================================================================================");
}

return 1;
}
}
}```
```                                 United States of America
====================================================================================================
# Abbrv State Name        SqrMls     Km2    Date        Order  Capital         Region
----------------------------------------------------------------------------------------------------
1  RI   Rhode Island       1545      4002   05/29/1790    13   Providence      New England
2  OH   Ohio              44828    116103   03/01/1803    17   Columbus        East North Central
3  KY   Kentucky          40411    104665   06/01/1792    15   Frankfort       New England
4  IA   Iowa              56276    145754   12/28/1846    29   Des Moines      West North Central
5  WI   Wisconsin         65503    169653   05/29/1848    30   Madison         East North Central
6  VT   Vermont            9615     24903   03/04/1791    14   Montpelier      New England
7  ID   Idaho             83574    216456   07/03/1890    43   Boise           Mountain
8  ME   Maine             35387     91653   03/15/1820    23   Augusta         New England
9  OR   Oregon            98386    254819   02/14/1859    33   Salem           Pacific
10  ND   North Dakota      70704    183123   11/02/1889    39   Bismarck        West North Central
11  IN   Indiana           36420     94328   12/11/1816    19   Indianapolis    East North Central
12  MS   Mississippi       48434    125443   12/10/1817    20   Jackson         East South Central
13  TX   Texas            268601    695676   12/29/1845    28   Austin          West South Central
14  MT   Montana          147046    380850   11/08/1889    41   Helena          Mountain
15  NC   North Carolina    53821    139397   11/21/1789    12   Raleigh         South Atlantic
16  TN   Tennessee         42146    109158   06/01/1796    16   Nashville       East South Central
17  NE   Nebraska          77358    200358   03/01/1867    37   Lincoln         West North Central
18  IL   Illinois          57918    150007   12/03/1818    21   Springfield     East North Central
19  KS   Kansas            82282    213110   01/29/1861    34   Topeka          West North Central
20  NH   New Hampshire      9351     24219   06/21/1788     9   Concord         New England
21  DE   Delaware           2489      6447   12/07/1787     1   Dover           South Atlantic
22  NJ   New Jersey         8722     22590   12/18/1787     3   Trenton         Mid-Atlantic
23  AK   Alaska           656424   1700139   01/03/1959    49   Juneau          Pacific
24  NM   New Mexico       121598    314939   01/06/1912    47   Santa Fe        Mountain
25  NY   New York          54475    141089   07/26/1788    11   Albany          Mid-Atlantic
26  CA   California       163707    424002   09/09/1850    31   Sacramento      Pacific
27  MO   Missouri          69709    180546   08/10/1821    24   Jefferson City  West North Central
28  OK   Oklahoma          69903    181049   11/16/1907    46   Oklahoma City   West South Central
29  PA   Pennsylvania      46058    119291   12/12/1787     2   Harrisburg      Mid-Atlantic
30  SC   South Carolina    32007     82898   05/23/1788     8   Columbia        South Atlantic
31  WY   Wyoming           97818    253349   07/10/1890    44   Cheyenne        Mountain
32  SD   South Dakota      77122    199745   11/02/1889    40   Pierre          West North Central
33  UT   Utah              84904    219902   01/04/1896    45   Salt Lake City  Mountain
34  AL   Alabama           52423    135775   12/14/1819    22   Montgomery      East South Central
35  VT   Vermont            9615     24903   03/04/1791    14   Montpelier      New England
36  AR   Arkansas          53182    137742   06/15/1836    25   Little Rock     West South Central
37  WA   Washington        71303    184674   11/11/1889    42   Olympia         Pacific
38  AZ   Arizona          114006    295276   02/14/1912    48   Phoenix         Mountain
39  WV   West Virginia     24231     62759   06/20/1863    35   Charleston      South Atlantic
40  LA   Louisiana         51844    134275   04/30/1812    18   Baton Rouge     West South Central
41  MA   Massachusetts     10555     27337   02/06/1788     6   Boston          New England
42  FL   Florida           65758    170313   03/03/1845    27   Tallahassee     South Atlantic
43  GA   Georgia           59441    153953   01/02/1788     4   Atlanta         South Atlantic
44  HI   Hawaii            10932     28313   08/21/1959    50   Honolulu        Pacific
45  MD   Maryland          12407     32135   04/28/1788     7   Annapolis       South Atlantic
====================================================================================================
Do you want to sort the list in alphabetical order (y/n)?```
3. When prompted, type y and press Enter:
```                                 United States of America
====================================================================================================
# Abbrv State Name        SqrMls     Km2    Date        Order  Capital         Region
----------------------------------------------------------------------------------------------------
1  AL   Alabama           52423    135775   12/14/1819    22   Montgomery      East South Central
2  AK   Alaska           656424   1700139   01/03/1959    49   Juneau          Pacific
3  AZ   Arizona          114006    295276   02/14/1912    48   Phoenix         Mountain
4  AR   Arkansas          53182    137742   06/15/1836    25   Little Rock     West South Central
5  CA   California       163707    424002   09/09/1850    31   Sacramento      Pacific
6  DE   Delaware           2489      6447   12/07/1787     1   Dover           South Atlantic
7  FL   Florida           65758    170313   03/03/1845    27   Tallahassee     South Atlantic
8  GA   Georgia           59441    153953   01/02/1788     4   Atlanta         South Atlantic
9  HI   Hawaii            10932     28313   08/21/1959    50   Honolulu        Pacific
10  ID   Idaho             83574    216456   07/03/1890    43   Boise           Mountain
11  IL   Illinois          57918    150007   12/03/1818    21   Springfield     East North Central
12  IN   Indiana           36420     94328   12/11/1816    19   Indianapolis    East North Central
13  IA   Iowa              56276    145754   12/28/1846    29   Des Moines      West North Central
14  KS   Kansas            82282    213110   01/29/1861    34   Topeka          West North Central
15  KY   Kentucky          40411    104665   06/01/1792    15   Frankfort       New England
16  LA   Louisiana         51844    134275   04/30/1812    18   Baton Rouge     West South Central
17  ME   Maine             35387     91653   03/15/1820    23   Augusta         New England
18  MD   Maryland          12407     32135   04/28/1788     7   Annapolis       South Atlantic
19  MA   Massachusetts     10555     27337   02/06/1788     6   Boston          New England
20  MS   Mississippi       48434    125443   12/10/1817    20   Jackson         East South Central
21  MO   Missouri          69709    180546   08/10/1821    24   Jefferson City  West North Central
22  MT   Montana          147046    380850   11/08/1889    41   Helena          Mountain
23  NE   Nebraska          77358    200358   03/01/1867    37   Lincoln         West North Central
24  NH   New Hampshire      9351     24219   06/21/1788     9   Concord         New England
25  NJ   New Jersey         8722     22590   12/18/1787     3   Trenton         Mid-Atlantic
26  NM   New Mexico       121598    314939   01/06/1912    47   Santa Fe        Mountain
27  NY   New York          54475    141089   07/26/1788    11   Albany          Mid-Atlantic
28  NC   North Carolina    53821    139397   11/21/1789    12   Raleigh         South Atlantic
29  ND   North Dakota      70704    183123   11/02/1889    39   Bismarck        West North Central
30  OH   Ohio              44828    116103   03/01/1803    17   Columbus        East North Central
31  OK   Oklahoma          69903    181049   11/16/1907    46   Oklahoma City   West South Central
32  OR   Oregon            98386    254819   02/14/1859    33   Salem           Pacific
33  PA   Pennsylvania      46058    119291   12/12/1787     2   Harrisburg      Mid-Atlantic
34  RI   Rhode Island       1545      4002   05/29/1790    13   Providence      New England
35  SC   South Carolina    32007     82898   05/23/1788     8   Columbia        South Atlantic
36  SD   South Dakota      77122    199745   11/02/1889    40   Pierre          West North Central
37  TN   Tennessee         42146    109158   06/01/1796    16   Nashville       East South Central
38  TX   Texas            268601    695676   12/29/1845    28   Austin          West South Central
39  UT   Utah              84904    219902   01/04/1896    45   Salt Lake City  Mountain
40  VT   Vermont            9615     24903   03/04/1791    14   Montpelier      New England
41  VT   Vermont            9615     24903   03/04/1791    14   Montpelier      New England
42  WA   Washington        71303    184674   11/11/1889    42   Olympia         Pacific
43  WV   West Virginia     24231     62759   06/20/1863    35   Charleston      South Atlantic
44  WI   Wisconsin         65503    169653   05/29/1848    30   Madison         East North Central
45  WY   Wyoming           97818    253349   07/10/1890    44   Cheyenne        Mountain
====================================================================================================
Press any key to continue . . .```

Copying an Array

Copying an array consists of assigning its values or objects to another array. To support this operation, the Array class provides various methods. The classic technique uses a method named Copy. It is overloaded in four versions for different scenarios. The easiest way to copy an array is through a version that takes three arguments. The first version takes three arguments: the source array, the destination array, and the number of items that will be copied.

Practical Learning: Copying an Array

1. Change the code as follows:
```using System;

namespace CountriesStatistics3
{
public class CountriesStatistics
{
private static void Display(Array entities)
{
Console.Clear();

Console.WriteLine("                                 United States of America");
Console.WriteLine("====================================================================================================");
Console.WriteLine(" # Abbrv State Name        SqrMls     Km2    Date        Order  Capital         Region");
Console.WriteLine("----------------------------------------------------------------------------------------------------");

for (int counter = 0; counter <= entities.Length - 1; counter++)
{
Federation stt = (Federation)entities.GetValue(counter);

Console.WriteLine("{0,2}  {1,2}   {2}   {3,6}   {4,7}   {5,8} {6,5}   {7}  {8}",
counter + 1, stt.Abbreviation, stt.StateName,
}
Console.WriteLine("====================================================================================================");
}

public static int Main(string[] args)
{
int counter = 0;

Region[] regions = new Region[]
{
new Region() { Designation = "East North Central", Description = "The East North Central region includes the states around the Great Lakes." },
new Region() { Designation = "East South Central", Description = "The East South Central portion is one of the regions designated as the South." },
new Region() { Designation = "New England       ", Description = "New England is the group of states in the North-East region. It is delimited in the North and North-East by Canada, in the East by the Atlantic Ocean, and in the South and West by the New York state." },
new Region() { Designation = "Mid-Atlantic      ", Description = "Mid-Atlantic is a region situated in the south of New England. Mid-Atlantic is one of the regions defined by the Census bureau for statistical purposes." },
new Region() { Designation = "Mountain          ", Description = "Like the name suggests, the Mountain region covers states known for their mountaneous characteristics. They are also covered by desertic areas." },
new Region() { Designation = "Pacific           ", Description = "The Pacific region covers the costal western states plus the two non-continental states of Alaska and the Hawaiian islands. All states in this region have a coast on the Pacific Ocean." },
new Region() { Designation = "South Atlantic    ", Description = "The South Atlantic region includes the states in the South-East part but also counts the Disctrict of Columbia." },
new Region() { Designation = "West North Central", Description = "The West North Central region includes the states in the Great Planes area. This reqion is divided from the East North Central part by the Mississippi River. This region is characterized by vast agricultural farms and high employment." },
new Region() { Designation = "West South Central", Description = "The West South Central part is one of the regions with (only) four states. The imposing Texas state is both the largest and the most populous state in the region." }
};

Federation[] states = new Federation[40];

states[0]  = new Federation() { Abbreviation = "RI", Name = "Rhode Island  ", Area =   1545, AreaSqrKms =    4002, AdmissionUnionDate = "05/29/1790", AdmissionUnionOrder = 13, Capital = "Providence    ", Region = regions[2] };
states[1]  = new Federation() { Abbreviation = "OH", Name = "Ohio          ", Area =  44828, AreaSqrKms =  116103, AdmissionUnionDate = "03/01/1803", AdmissionUnionOrder = 17, Capital = "Columbus      ", Region = regions[0] };
states[2]  = new Federation() { Abbreviation = "KY", Name = "Kentucky      ", Area =  40411, AreaSqrKms =  104665, AdmissionUnionDate = "06/01/1792", AdmissionUnionOrder = 15, Capital = "Frankfort     ", Region = regions[2] };
states[3]  = new Federation() { Abbreviation = "IA", Name = "Iowa          ", Area =  56276, AreaSqrKms =  145754, AdmissionUnionDate = "12/28/1846", AdmissionUnionOrder = 29, Capital = "Des Moines    ", Region = regions[7] };
states[4]  = new Federation() { Abbreviation = "WI", Name = "Wisconsin     ", Area =  65503, AreaSqrKms =  169653, AdmissionUnionDate = "05/29/1848", AdmissionUnionOrder = 30, Capital = "Madison       ", Region = regions[0] };
states[5]  = new Federation() { Abbreviation = "VT", Name = "Vermont       ", Area =   9615, AreaSqrKms =   24903, AdmissionUnionDate = "03/04/1791", AdmissionUnionOrder = 14, Capital = "Montpelier    ", Region = regions[2] };
states[6]  = new Federation() { Abbreviation = "ID", Name = "Idaho         ", Area =  83574, AreaSqrKms =  216456, AdmissionUnionDate = "07/03/1890", AdmissionUnionOrder = 43, Capital = "Boise         ", Region = regions[4] };
states[7]  = new Federation() { Abbreviation = "ME", Name = "Maine         ", Area =  35387, AreaSqrKms =   91653, AdmissionUnionDate = "03/15/1820", AdmissionUnionOrder = 23, Capital = "Augusta       ", Region = regions[2] };
states[8]  = new Federation() { Abbreviation = "OR", Name = "Oregon        ", Area =  98386, AreaSqrKms =  254819, AdmissionUnionDate = "02/14/1859", AdmissionUnionOrder = 33, Capital = "Salem         ", Region = regions[5] };
states[9]  = new Federation() { Abbreviation = "ND", Name = "North Dakota  ", Area =  70704, AreaSqrKms =  183123, AdmissionUnionDate = "11/02/1889", AdmissionUnionOrder = 39, Capital = "Bismarck      ", Region = regions[7] };
states[10] = new Federation() { Abbreviation = "IN", Name = "Indiana       ", Area =  36420, AreaSqrKms =   94328, AdmissionUnionDate = "12/11/1816", AdmissionUnionOrder = 19, Capital = "Indianapolis  ", Region = regions[0] };
states[11] = new Federation() { Abbreviation = "MS", Name = "Mississippi   ", Area =  48434, AreaSqrKms =  125443, AdmissionUnionDate = "12/10/1817", AdmissionUnionOrder = 20, Capital = "Jackson       ", Region = regions[1] };
states[12] = new Federation() { Abbreviation = "TX", Name = "Texas         ", Area = 268601, AreaSqrKms =  695676, AdmissionUnionDate = "12/29/1845", AdmissionUnionOrder = 28, Capital = "Austin        ", Region = regions[8] };
states[13] = new Federation() { Abbreviation = "MT", Name = "Montana       ", Area = 147046, AreaSqrKms =  380850, AdmissionUnionDate = "11/08/1889", AdmissionUnionOrder = 41, Capital = "Helena        ", Region = regions[4] };
states[14] = new Federation() { Abbreviation = "NC", Name = "North Carolina", Area =  53821, AreaSqrKms =  139397, AdmissionUnionDate = "11/21/1789", AdmissionUnionOrder = 12, Capital = "Raleigh       ", Region = regions[6] };
states[15] = new Federation() { Abbreviation = "TN", Name = "Tennessee     ", Area =  42146, AreaSqrKms =  109158, AdmissionUnionDate = "06/01/1796", AdmissionUnionOrder = 16, Capital = "Nashville     ", Region = regions[1] };
states[16] = new Federation() { Abbreviation = "NE", Name = "Nebraska      ", Area =  77358, AreaSqrKms =  200358, AdmissionUnionDate = "03/01/1867", AdmissionUnionOrder = 37, Capital = "Lincoln       ", Region = regions[7] };
states[17] = new Federation() { Abbreviation = "IL", Name = "Illinois      ", Area =  57918, AreaSqrKms =  150007, AdmissionUnionDate = "12/03/1818", AdmissionUnionOrder = 21, Capital = "Springfield   ", Region = regions[0] };
states[18] = new Federation() { Abbreviation = "KS", Name = "Kansas        ", Area =  82282, AreaSqrKms =  213110, AdmissionUnionDate = "01/29/1861", AdmissionUnionOrder = 34, Capital = "Topeka        ", Region = regions[7] };
states[19] = new Federation() { Abbreviation = "NH", Name = "New Hampshire ", Area =   9351, AreaSqrKms =   24219, AdmissionUnionDate = "06/21/1788", AdmissionUnionOrder =  9, Capital = "Concord       ", Region = regions[2] };
states[20] = new Federation() { Abbreviation = "DE", Name = "Delaware      ", Area =   2489, AreaSqrKms =    6447, AdmissionUnionDate = "12/07/1787", AdmissionUnionOrder =  1, Capital = "Dover         ", Region = regions[6] };
states[21] = new Federation() { Abbreviation = "NJ", Name = "New Jersey    ", Area =   8722, AreaSqrKms =   22590, AdmissionUnionDate = "12/18/1787", AdmissionUnionOrder =  3, Capital = "Trenton       ", Region = regions[3] };
states[22] = new Federation() { Abbreviation = "AK", Name = "Alaska        ", Area = 656424, AreaSqrKms = 1700139, AdmissionUnionDate = "01/03/1959", AdmissionUnionOrder = 49, Capital = "Juneau        ", Region = regions[5] };
states[23] = new Federation() { Abbreviation = "NM", Name = "New Mexico    ", Area = 121598, AreaSqrKms =  314939, AdmissionUnionDate = "01/06/1912", AdmissionUnionOrder = 47, Capital = "Santa Fe      ", Region = regions[4] };
states[24] = new Federation() { Abbreviation = "NY", Name = "New York      ", Area =  54475, AreaSqrKms =  141089, AdmissionUnionDate = "07/26/1788", AdmissionUnionOrder = 11, Capital = "Albany        ", Region = regions[3] };
states[25] = new Federation() { Abbreviation = "CA", Name = "California    ", Area = 163707, AreaSqrKms =  424002, AdmissionUnionDate = "09/09/1850", AdmissionUnionOrder = 31, Capital = "Sacramento    ", Region = regions[5] };
states[26] = new Federation() { Abbreviation = "MO", Name = "Missouri      ", Area =  69709, AreaSqrKms =  180546, AdmissionUnionDate = "08/10/1821", AdmissionUnionOrder = 24, Capital = "Jefferson City", Region = regions[7] };
states[27] = new Federation() { Abbreviation = "OK", Name = "Oklahoma      ", Area =  69903, AreaSqrKms =  181049, AdmissionUnionDate = "11/16/1907", AdmissionUnionOrder = 46, Capital = "Oklahoma City ", Region = regions[8] };
states[28] = new Federation() { Abbreviation = "PA", Name = "Pennsylvania  ", Area =  46058, AreaSqrKms =  119291, AdmissionUnionDate = "12/12/1787", AdmissionUnionOrder =  2, Capital = "Harrisburg    ", Region = regions[3] };
states[29] = new Federation() { Abbreviation = "SC", Name = "South Carolina", Area =  32007, AreaSqrKms =   82898, AdmissionUnionDate = "05/23/1788", AdmissionUnionOrder =  8, Capital = "Columbia      ", Region = regions[6] };
states[30] = new Federation() { Abbreviation = "WY", Name = "Wyoming       ", Area =  97818, AreaSqrKms =  253349, AdmissionUnionDate = "07/10/1890", AdmissionUnionOrder = 44, Capital = "Cheyenne      ", Region = regions[4] };
states[31] = new Federation() { Abbreviation = "SD", Name = "South Dakota  ", Area =  77122, AreaSqrKms =  199745, AdmissionUnionDate = "11/02/1889", AdmissionUnionOrder = 40, Capital = "Pierre        ", Region = regions[7] };
states[32] = new Federation() { Abbreviation = "UT", Name = "Utah          ", Area =  84904, AreaSqrKms =  219902, AdmissionUnionDate = "01/04/1896", AdmissionUnionOrder = 45, Capital = "Salt Lake City", Region = regions[4] };
states[33] = new Federation() { Abbreviation = "AL", Name = "Alabama       ", Area =  52423, AreaSqrKms =  135775, AdmissionUnionDate = "12/14/1819", AdmissionUnionOrder = 22, Capital = "Montgomery    ", Region = regions[1] };
states[34] = new Federation() { Abbreviation = "VT", Name = "Vermont       ", Area =   9615, AreaSqrKms =   24903, AdmissionUnionDate = "03/04/1791", AdmissionUnionOrder = 14, Capital = "Montpelier    ", Region = regions[2] };
states[35] = new Federation() { Abbreviation = "AR", Name = "Arkansas      ", Area =  53182, AreaSqrKms =  137742, AdmissionUnionDate = "06/15/1836", AdmissionUnionOrder = 25, Capital = "Little Rock   ", Region = regions[8] };
states[36] = new Federation() { Abbreviation = "WA", Name = "Washington    ", Area =  71303, AreaSqrKms =  184674, AdmissionUnionDate = "11/11/1889", AdmissionUnionOrder = 42, Capital = "Olympia       ", Region = regions[5] };
states[37] = new Federation() { Abbreviation = "AZ", Name = "Arizona       ", Area = 114006, AreaSqrKms =  295276, AdmissionUnionDate = "02/14/1912", AdmissionUnionOrder = 48, Capital = "Phoenix       ", Region = regions[4] };
states[38] = new Federation() { Abbreviation = "WV", Name = "West Virginia ", Area =  24231, AreaSqrKms =   62759, AdmissionUnionDate = "06/20/1863", AdmissionUnionOrder = 35, Capital = "Charleston    ", Region = regions[6] };
states[39] = new Federation() { Abbreviation = "LA", Name = "Louisiana     ", Area =  51844, AreaSqrKms =  134275, AdmissionUnionDate = "04/30/1812", AdmissionUnionOrder = 18, Capital = "Baton Rouge   ", Region = regions[8] };

Array.Resize(ref states, states.Length + 5);

states[40] = new Federation() { Abbreviation = "MA", Name = "Massachusetts ", Area =  10555, AreaSqrKms =   27337, AdmissionUnionDate = "02/06/1788", AdmissionUnionOrder =  6, Capital = "Boston        ", Region = regions[2] };
states[41] = new Federation() { Abbreviation = "FL", Name = "Florida       ", Area =  65758, AreaSqrKms =  170313, AdmissionUnionDate = "03/03/1845", AdmissionUnionOrder = 27, Capital = "Tallahassee   ", Region = regions[6] };
states[42] = new Federation() { Abbreviation = "GA", Name = "Georgia       ", Area =  59441, AreaSqrKms =  153953, AdmissionUnionDate = "01/02/1788", AdmissionUnionOrder =  4, Capital = "Atlanta       ", Region = regions[6] };
states[43] = new Federation() { Abbreviation = "HI", Name = "Hawaii        ", Area =  10932, AreaSqrKms =   28313, AdmissionUnionDate = "08/21/1959", AdmissionUnionOrder = 50, Capital = "Honolulu      ", Region = regions[5] };
states[44] = new Federation() { Abbreviation = "MD", Name = "Maryland      ", Area =  12407, AreaSqrKms =   32135, AdmissionUnionDate = "04/28/1788", AdmissionUnionOrder =  7, Capital = "Annapolis     ", Region = regions[6] };

Federation[] temporary = new Federation[states.Length + 5];

Array.Copy(states, temporary, states.Length);

states = temporary;

states[45] = new Federation() { Abbreviation = "CO", Name = "Colorado      ", Area = 104100, AreaSqrKms =  269620, AdmissionUnionDate = "08/01/1876", AdmissionUnionOrder = 38, Capital = "Denver        ", Region = regions[4] };
states[46] = new Federation() { Abbreviation = "MI", Name = "Michigan      ", Area =  98810, AreaSqrKms =  250738, AdmissionUnionDate = "01/26/1837", AdmissionUnionOrder = 26, Capital = "Lansing       ", Region = regions[0] };
states[47] = new Federation() { Abbreviation = "MN", Name = "Minnesota     ", Area =  86943, AreaSqrKms =  225182, AdmissionUnionDate = "05/11/1858", AdmissionUnionOrder = 32, Capital = "Saint Paul    ", Region = regions[7] };
states[48] = new Federation() { Abbreviation = "CT", Name = "Connecticut   ", Area =   5544, AreaSqrKms =   14358, AdmissionUnionDate = "01/09/1788", AdmissionUnionOrder =  5, Capital = "Hartford      ", Region = regions[3] };
states[49] = new Federation() { Abbreviation = "NV", Name = "Nevada        ", Area = 110567, AreaSqrKms =  286368, AdmissionUnionDate = "10/31/1864", AdmissionUnionOrder = 36, Capital = "Frankfort     ", Region = regions[4] };

Display(states);

Console.Write("Do you want to sort the list in alphabetical order (y/n)? ");

{
Array.Sort(states);
Display(states);
}
else
{
Console.WriteLine("Good Bye!!!");
Console.WriteLine("====================================================================================================");
}

return 1;
}
}
}```
```                                 United States of America
====================================================================================================
# Abbrv State Name        SqrMls     Km2    Date        Order  Capital         Region
----------------------------------------------------------------------------------------------------
1  RI   Rhode Island       1545      4002   05/29/1790    13   Providence      New England
2  OH   Ohio              44828    116103   03/01/1803    17   Columbus        East North Central
3  KY   Kentucky          40411    104665   06/01/1792    15   Frankfort       New England
4  IA   Iowa              56276    145754   12/28/1846    29   Des Moines      West North Central
5  WI   Wisconsin         65503    169653   05/29/1848    30   Madison         East North Central
6  VT   Vermont            9615     24903   03/04/1791    14   Montpelier      New England
7  ID   Idaho             83574    216456   07/03/1890    43   Boise           Mountain
8  ME   Maine             35387     91653   03/15/1820    23   Augusta         New England
9  OR   Oregon            98386    254819   02/14/1859    33   Salem           Pacific
10  ND   North Dakota      70704    183123   11/02/1889    39   Bismarck        West North Central
11  IN   Indiana           36420     94328   12/11/1816    19   Indianapolis    East North Central
12  MS   Mississippi       48434    125443   12/10/1817    20   Jackson         East South Central
13  TX   Texas            268601    695676   12/29/1845    28   Austin          West South Central
14  MT   Montana          147046    380850   11/08/1889    41   Helena          Mountain
15  NC   North Carolina    53821    139397   11/21/1789    12   Raleigh         South Atlantic
16  TN   Tennessee         42146    109158   06/01/1796    16   Nashville       East South Central
17  NE   Nebraska          77358    200358   03/01/1867    37   Lincoln         West North Central
18  IL   Illinois          57918    150007   12/03/1818    21   Springfield     East North Central
19  KS   Kansas            82282    213110   01/29/1861    34   Topeka          West North Central
20  NH   New Hampshire      9351     24219   06/21/1788     9   Concord         New England
21  DE   Delaware           2489      6447   12/07/1787     1   Dover           South Atlantic
22  NJ   New Jersey         8722     22590   12/18/1787     3   Trenton         Mid-Atlantic
23  AK   Alaska           656424   1700139   01/03/1959    49   Juneau          Pacific
24  NM   New Mexico       121598    314939   01/06/1912    47   Santa Fe        Mountain
25  NY   New York          54475    141089   07/26/1788    11   Albany          Mid-Atlantic
26  CA   California       163707    424002   09/09/1850    31   Sacramento      Pacific
27  MO   Missouri          69709    180546   08/10/1821    24   Jefferson City  West North Central
28  OK   Oklahoma          69903    181049   11/16/1907    46   Oklahoma City   West South Central
29  PA   Pennsylvania      46058    119291   12/12/1787     2   Harrisburg      Mid-Atlantic
30  SC   South Carolina    32007     82898   05/23/1788     8   Columbia        South Atlantic
31  WY   Wyoming           97818    253349   07/10/1890    44   Cheyenne        Mountain
32  SD   South Dakota      77122    199745   11/02/1889    40   Pierre          West North Central
33  UT   Utah              84904    219902   01/04/1896    45   Salt Lake City  Mountain
34  AL   Alabama           52423    135775   12/14/1819    22   Montgomery      East South Central
35  VT   Vermont            9615     24903   03/04/1791    14   Montpelier      New England
36  AR   Arkansas          53182    137742   06/15/1836    25   Little Rock     West South Central
37  WA   Washington        71303    184674   11/11/1889    42   Olympia         Pacific
38  AZ   Arizona          114006    295276   02/14/1912    48   Phoenix         Mountain
39  WV   West Virginia     24231     62759   06/20/1863    35   Charleston      South Atlantic
40  LA   Louisiana         51844    134275   04/30/1812    18   Baton Rouge     West South Central
41  MA   Massachusetts     10555     27337   02/06/1788     6   Boston          New England
42  FL   Florida           65758    170313   03/03/1845    27   Tallahassee     South Atlantic
43  GA   Georgia           59441    153953   01/02/1788     4   Atlanta         South Atlantic
44  HI   Hawaii            10932     28313   08/21/1959    50   Honolulu        Pacific
45  MD   Maryland          12407     32135   04/28/1788     7   Annapolis       South Atlantic
46  CO   Colorado         104100    269620   08/01/1876    38   Denver          Mountain
47  MI   Michigan          98810    250738   01/26/1837    26   Lansing         East North Central
48  MN   Minnesota         86943    225182   05/11/1858    32   Saint Paul      West North Central
49  CT   Connecticut        5544     14358   01/09/1788     5   Hartford        Mid-Atlantic
50  NV   Nevada           110567    286368   10/31/1864    36   Frankfort       Mountain
====================================================================================================
Do you want to sort the list in alphabetical order (y/n)?```
3. When prompted, type y and press Enter:
```                                 United States of America
====================================================================================================
# Abbrv State Name        SqrMls     Km2    Date        Order  Capital         Region
----------------------------------------------------------------------------------------------------
1  AL   Alabama           52423    135775   12/14/1819    22   Montgomery      East South Central
2  AK   Alaska           656424   1700139   01/03/1959    49   Juneau          Pacific
3  AZ   Arizona          114006    295276   02/14/1912    48   Phoenix         Mountain
4  AR   Arkansas          53182    137742   06/15/1836    25   Little Rock     West South Central
5  CA   California       163707    424002   09/09/1850    31   Sacramento      Pacific
6  CO   Colorado         104100    269620   08/01/1876    38   Denver          Mountain
7  CT   Connecticut        5544     14358   01/09/1788     5   Hartford        Mid-Atlantic
8  DE   Delaware           2489      6447   12/07/1787     1   Dover           South Atlantic
9  FL   Florida           65758    170313   03/03/1845    27   Tallahassee     South Atlantic
10  GA   Georgia           59441    153953   01/02/1788     4   Atlanta         South Atlantic
11  HI   Hawaii            10932     28313   08/21/1959    50   Honolulu        Pacific
12  ID   Idaho             83574    216456   07/03/1890    43   Boise           Mountain
13  IL   Illinois          57918    150007   12/03/1818    21   Springfield     East North Central
14  IN   Indiana           36420     94328   12/11/1816    19   Indianapolis    East North Central
15  IA   Iowa              56276    145754   12/28/1846    29   Des Moines      West North Central
16  KS   Kansas            82282    213110   01/29/1861    34   Topeka          West North Central
17  KY   Kentucky          40411    104665   06/01/1792    15   Frankfort       New England
18  LA   Louisiana         51844    134275   04/30/1812    18   Baton Rouge     West South Central
19  ME   Maine             35387     91653   03/15/1820    23   Augusta         New England
20  MD   Maryland          12407     32135   04/28/1788     7   Annapolis       South Atlantic
21  MA   Massachusetts     10555     27337   02/06/1788     6   Boston          New England
22  MI   Michigan          98810    250738   01/26/1837    26   Lansing         East North Central
23  MN   Minnesota         86943    225182   05/11/1858    32   Saint Paul      West North Central
24  MS   Mississippi       48434    125443   12/10/1817    20   Jackson         East South Central
25  MO   Missouri          69709    180546   08/10/1821    24   Jefferson City  West North Central
26  MT   Montana          147046    380850   11/08/1889    41   Helena          Mountain
27  NE   Nebraska          77358    200358   03/01/1867    37   Lincoln         West North Central
28  NV   Nevada           110567    286368   10/31/1864    36   Frankfort       Mountain
29  NH   New Hampshire      9351     24219   06/21/1788     9   Concord         New England
30  NJ   New Jersey         8722     22590   12/18/1787     3   Trenton         Mid-Atlantic
31  NM   New Mexico       121598    314939   01/06/1912    47   Santa Fe        Mountain
32  NY   New York          54475    141089   07/26/1788    11   Albany          Mid-Atlantic
33  NC   North Carolina    53821    139397   11/21/1789    12   Raleigh         South Atlantic
34  ND   North Dakota      70704    183123   11/02/1889    39   Bismarck        West North Central
35  OH   Ohio              44828    116103   03/01/1803    17   Columbus        East North Central
36  OK   Oklahoma          69903    181049   11/16/1907    46   Oklahoma City   West South Central
37  OR   Oregon            98386    254819   02/14/1859    33   Salem           Pacific
38  PA   Pennsylvania      46058    119291   12/12/1787     2   Harrisburg      Mid-Atlantic
39  RI   Rhode Island       1545      4002   05/29/1790    13   Providence      New England
40  SC   South Carolina    32007     82898   05/23/1788     8   Columbia        South Atlantic
41  SD   South Dakota      77122    199745   11/02/1889    40   Pierre          West North Central
42  TN   Tennessee         42146    109158   06/01/1796    16   Nashville       East South Central
43  TX   Texas            268601    695676   12/29/1845    28   Austin          West South Central
44  UT   Utah              84904    219902   01/04/1896    45   Salt Lake City  Mountain
45  VT   Vermont            9615     24903   03/04/1791    14   Montpelier      New England
46  VT   Vermont            9615     24903   03/04/1791    14   Montpelier      New England
47  WA   Washington        71303    184674   11/11/1889    42   Olympia         Pacific
48  WV   West Virginia     24231     62759   06/20/1863    35   Charleston      South Atlantic
49  WI   Wisconsin         65503    169653   05/29/1848    30   Madison         East North Central
50  WY   Wyoming           97818    253349   07/10/1890    44   Cheyenne        Mountain
====================================================================================================
Press any key to continue . . .```

Finding an Item in an Array

The Array class provides various means of looking for, or locating, an element in an array.

To support binary search, the Array class is equipped with a method named BinarySearch that is overloaded in 8 versions. One of the versions uses the following syntax:

`public static int BinarySearch(Array array, object value);`

Before calling this method, you can sort the array. Here is an example of calling it:

```using System;

public class Exercise
{
public static void Main()
{
int[] numbers = new[] { 102, 44, 525, 38, 6, 28, 24481, 327, 632, 104 };

Console.WriteLine("List of Numbers");

Array.Sort(numbers);

foreach (var number in numbers)
Console.WriteLine("Number: " + number);
Console.WriteLine("-------------------------");

int index = Array.BinarySearch(numbers, 525);

Console.WriteLine("The index of 525 is " + index);

Console.WriteLine("=================================");
}
}```

This would produce:

```List of Numbers
Number: 6
Number: 28
Number: 38
Number: 44
Number: 102
Number: 104
Number: 327
Number: 525
Number: 632
Number: 24481
-------------------------
The index of 525 is 7
=================================
Press any key to continue . . .```

Locating the Index of an Element

One of the most routine operations you can perform on an array is to find out whether it contains this or that value. For example, if the array contains a certain member, you may want to retrieve the index of that member. To assist you with this, the Array class is equipped with a method named IndexOf() method that comes in various versions. To apply it on the type of array we have used so far, you can use the following syntax:

`public static int IndexOf(Array array, object value);`

This method visits each member of the array, looking for the value. Once it finds value in the array, it stops and returns the index where the first occurrence of value was found. If the Array.IndexOf() method finds the value in the array, it returns its position. Here is an example of calling it:

```using System;

public class Exercise
{
public static void Main()
{
double[] numbers = new[]
{
7628.937, 6.48, 574.9, 293749.064,
0.70257, 314.905, 80458.01
};

Console.WriteLine("List of Numbers");

foreach (var number in numbers)
Console.WriteLine("Number: " + number);
Console.WriteLine("---------------------------");

int index = Array.IndexOf(numbers, 314.905);

Console.WriteLine("The index of 314.905 is " + index);

Console.WriteLine("=================================");
}
}```

This would produce:

```List of Numbers
Number: 7628.937
Number: 6.48
Number: 574.9
Number: 293749.064
Number: 0.70257
Number: 314.905
Number: 80458.01
---------------------------
The index of 314.905 is 5
=================================
Press any key to continue . . .```

If the item is not found in the array, the method may return -1.

The IndexOf() method actually looks for the first occurrence of an item in an array. If you prefer to get the last occurrence of that item in the array, you can call the Array.LastIndexOf() method. It also is overloaded in three versions.

Deleting Items From an Array

Deleting an item array consists of removing it from the list. The Array class allows you to delete one element or a range of items from an array. To support these operations, the class is equipped with a static method named Clear. Its syntax is:

`public static void Clear(Array array, int index, int length);`

The first argument of this method is the name of the array on which the operation is performed. The second argument is the index of the item where the deletion will occur or start. If that second index exists in the array, the third argument specifies the number of items to remove. If an item is deleted, it receives a default value. For example, if it is a number, it gets a 0 value.

If you want to delete just one item, provide its index as the second argument and 1 as the third. Here is an example:

```using System;

public class Exercise
{
public static void Main()
{
double[] numbers = new[]
{
7628.937, 6.48, 574.9, 293749.064,
0.70257, 314.905, 80458.01
};

Console.WriteLine("Original List of Numbers");

foreach (var number in numbers)
Console.WriteLine("Number: " + number);
Console.WriteLine("---------------------------");

Array.Clear(numbers, 2, 1);

Console.WriteLine("Modified List of Numbers");

foreach (var number in numbers)
Console.WriteLine("Number: " + number);

Console.WriteLine("=================================");
}
}```

This would produce:

```Original List of Numbers
Number: 7628.937
Number: 6.48
Number: 574.9
Number: 293749.064
Number: 0.70257
Number: 314.905
Number: 80458.01
---------------------------
Modified List of Numbers
Number: 7628.937
Number: 6.48
Number: 0
Number: 293749.064
Number: 0.70257
Number: 314.905
Number: 80458.01
=================================
Press any key to continue . . .```

To delete a range of items, pass the starting index as the second argument and pass the length of the range (the number of items to delete from the starting range) as the second argument. Here is an example:

```using System;

public class Exercise
{
public static void Main()
{
double[] numbers = new[]
{
7628.937, 6.48, 574.9, 293749.064,
0.70257, 314.905, 80458.01
};

Console.WriteLine("Original List of Numbers");

foreach (var number in numbers)
Console.WriteLine("Number: " + number);
Console.WriteLine("---------------------------");

Array.Clear(numbers, 2, 3);

Console.WriteLine("Modified List of Numbers");

foreach (var number in numbers)
Console.WriteLine("Number: " + number);

Console.WriteLine("=================================");
}
}```

This would produce:

```Original List of Numbers
Number: 7628.937
Number: 6.48
Number: 574.9
Number: 293749.064
Number: 0.70257
Number: 314.905
Number: 80458.01
---------------------------
Modified List of Numbers
Number: 7628.937
Number: 6.48
Number: 0
Number: 0
Number: 0
Number: 314.905
Number: 80458.01
=================================
Press any key to continue . . .```

Clearing an array consists of deleting all of its elements and setting the count to 0. To do it, call the Array.Clear() method. Pass 0 as the second argument and the length of the array as the third argument. Here is an example:

```using System;

public class Exercise
{
public static void Main()
{
double[] numbers = new[]
{
7628.937, 6.48, 574.9, 293749.064,
0.70257, 314.905, 80458.01
};

Console.WriteLine("Original List of Numbers");

foreach (var number in numbers)
Console.WriteLine("Number: " + number);
Console.WriteLine("---------------------------");

Array.Clear(numbers, 0, numbers.Length);

Console.WriteLine("Modified List of Numbers");

foreach (var number in numbers)
Console.WriteLine("Number: " + number);

Console.WriteLine("=================================");
}
}```

This would produce:

```Original List of Numbers
Number: 7628.937
Number: 6.48
Number: 574.9
Number: 293749.064
Number: 0.70257
Number: 314.905
Number: 80458.01
---------------------------
Modified List of Numbers
Number: 0
Number: 0
Number: 0
Number: 0
Number: 0
Number: 0
Number: 0
=================================
Press any key to continue . . .```

Multidimensional Arrays

Two-Dimensional Arrays

The Array class supports the creation of any of the types of arrays we saw in the previous lessons. In the previous lesson, we saw that a two-dimensional array was an array made of two lists:

`var members = new string[List1Length, List2Length];`

To create such an array using the Array class, you can use the following version of the Array.CreateInstance() method:

`public static Array CreateInstance(Type elementType, int length1, int length2)`

The first argument is the type of array you want to create. The second argument is the length of the first list. The third argument is the length of the second list. Here are examples of using it:

```using System;

public class Exercise
{
public static void Main()
{
Array numbers = Array.CreateInstance(typeof(double), 6, 3);
var names = Array.CreateInstance(typeof(string), 2, 4);
dynamic distances = Array.CreateInstance(typeof(string), 5, 17);
}
}```

To specify the values of a two-dimensional array, you can use the following version of the Array.SetValue() method:

`public void SetValue(object value, int index1, int index2)`

The first argument is the value you want to add. The second argument is the index of the list. The second argument is the index of the element that is being added. Here is an example:

```using System;

public class Exercise
{
public static void Main()
{
var members = Array.CreateInstance(typeof(string), 2, 4);

members.SetValue("Celeste", 0, 0);  // 1st List - 1st Element
members.SetValue("Mathurin", 0, 1); // 1st List - 2nd Element
members.SetValue("Alex", 0, 2);     // 1st List - 3rd Element
members.SetValue("Germain", 0, 3);   // 1st List - 4th Element

members.SetValue("Jeremy", 1, 0);   // 2nd List - 1st Element
members.SetValue("Mathew", 1, 1);   // 1st List - 2nd Element
members.SetValue("Anselme", 1, 2);  // 1st List - 3rd Element
members.SetValue("Frederique", 1, 3);// 1st List - 4th Element
}
}```

Just as mentioned for the one-dimensional array, you can use the square brackets to create the array but call the SetValue() method to specify the value of each element.

To access a member of a two-dimensional array created with the Array.SetValue() method, you use the following version of the Array.GetValue() method:

`public object GetValue(int index1, int index2)`

This method takes two arguments. The first argument is the index of the list where the desired member resides. The second argument is the index of the element itself. Here is an example:

```using System;

public class Exercise
{
public static void Main()
{
var members = Array.CreateInstance(typeof(string), 2, 4);

members.SetValue("Celeste", 0, 0);  // 1st List - 1st Element
members.SetValue("Mathurin", 0, 1); // 1st List - 2nd Element
members.SetValue("Alex", 0, 2);     // 1st List - 3rd Element
members.SetValue("Germain", 0, 3);   // 1st List - 4th Element

members.SetValue("Jeremy", 1, 0);   // 2nd List - 1st Element
members.SetValue("Mathew", 1, 1);   // 1st List - 2nd Element
members.SetValue("Anselme", 1, 2);  // 1st List - 3rd Element
members.SetValue("Frederique", 1, 3);// 1st List - 4th Element

Console.WriteLine("Member: " + members.GetValue(0, 2));
Console.WriteLine("=================================");
}
}```

To access each member of the list, you can use two for loops. Use the first loop to access each list. Nest a second loop to it to access each member. To get the dimension of the main list, you can call the Array.GetLength() method and specify its argument as 0. For the internal loop, pass 1 as the argument to the Array.GetLength() method. Here is an example:

```using System;

public class Exercise
{
public static void Main()
{
var members = Array.CreateInstance(typeof(string), 2, 4);

members.SetValue("Celeste", 0, 0);  // 1st List - 1st Element
members.SetValue("Mathurin", 0, 1); // 1st List - 2nd Element
members.SetValue("Alex", 0, 2);     // 1st List - 3rd Element
members.SetValue("Germain", 0, 3);   // 1st List - 4th Element

members.SetValue("Jeremy", 1, 0);   // 2nd List - 1st Element
members.SetValue("Mathew", 1, 1);   // 1st List - 2nd Element
members.SetValue("Anselme", 1, 2);  // 1st List - 3rd Element
members.SetValue("Frederique", 1, 3);// 1st List - 4th Element

for (int list = 0; list < members.GetLength(0); list++)
for (int counter = 0; counter < members.GetLength(1); counter++)
Console.WriteLine("Member: " + members.GetValue(list, counter));

Console.WriteLine("=================================");
}
}```

This would produce:

```Member: Celeste
Member: Mathurin
Member: Alex
Member: Germain
Member: Jeremy
Member: Mathew
Member: Anselme
Member: Frederique
=================================
Press any key to continue . . .```

You can also use a foreach operator to access each member of the array. When using it, there is no need for a counter. Here is an example:

```using System;

public class Exercise
{
public static void Main()
{
var members = Array.CreateInstance(typeof(string), 2, 4);

members.SetValue("Celeste", 0, 0);  // 1st List - 1st Element
members.SetValue("Mathurin", 0, 1); // 1st List - 2nd Element
members.SetValue("Alex", 0, 2);     // 1st List - 3rd Element
members.SetValue("Germain", 0, 3);   // 1st List - 4th Element

members.SetValue("Jeremy", 1, 0);   // 2nd List - 1st Element
members.SetValue("Mathew", 1, 1);   // 1st List - 2nd Element
members.SetValue("Anselme", 1, 2);  // 1st List - 3rd Element
members.SetValue("Frederique", 1, 3);// 1st List - 4th Element

foreach (var member in members)
Console.WriteLine("Member: " + member);

Console.WriteLine("=================================");
}
}```

Three-Dimensional Arrays

Instead of two dimensions, you may want to create a three-dimensional arrays. A 3-D array is an array that, if created with the square brackets, would use two commas. Here is an example:

```public class Exercise
{
public static void Main()
{
double[,,] number;
}
}```

To create such an array using the Array class, you can usethe following version of its CreateInstance() method:

```public static Array CreateInstance(Type elementType,
int length1,
int length2,
int length3)```

Here is an example:

```@{
var number = Array.CreateInstance(typeof(double), 2, 3, 5);
}```

To specify the value of each member of the three-dimensional array, you can call the following version of the Array.SetValue() method:

```public void SetValue(Object value,
int index1,
int index2,
int index3)```

Here is an example:

```using System;

public class Exercise
{
public static void Main()
{
var number = Array.CreateInstance(typeof(double), 2, 3, 5);

number.SetValue(12.44, 0, 0, 0);
number.SetValue(525.38, 0, 0, 1);
number.SetValue(-6.28, 0, 0, 2);

. . .

number.SetValue(-166.07, 1, 2, 2);
number.SetValue(4444.26, 1, 2, 3);
number.SetValue(408.62, 1, 2, 4);
}
}```

To get the value of each member of the three-dimensional array, you can call the following version of the Array.GetValue() method:

```public Object GetValue(int index1,
int index2,
int index3)```

Here is an example:

```using System;

public class Exercise
{
public static void Main()
{
var number = Array.CreateInstance(typeof(double), 2, 3, 5);

number.SetValue(12.44, 0, 0, 0);
number.SetValue(525.38, 0, 0, 1);
number.SetValue(-6.28, 0, 0, 2);
number.SetValue(2448.32, 0, 0, 3);
number.SetValue(632.04, 0, 0, 4);
number.SetValue(-378.05, 0, 1, 0);
number.SetValue(48.14, 0, 1, 1);
number.SetValue(634.18, 0, 1, 2);
number.SetValue(762.48, 0, 1, 3);
number.SetValue(83.02, 0, 1, 4);
number.SetValue(64.92, 0, 2, 0);
number.SetValue(-7.44, 0, 2, 1);
number.SetValue(86.74, 0, 2, 2);
number.SetValue(-534.60, 0, 2, 3);
number.SetValue(386.73, 0, 2, 4);
number.SetValue(48.02, 1, 0, 0);
number.SetValue(120.44, 1, 0, 1);
number.SetValue(38.62, 1, 0, 2);
number.SetValue(526.82, 1, 0, 3);
number.SetValue(1704.62, 1, 0, 4);
number.SetValue(56.85, 1, 1, 0);
number.SetValue(105.48, 1, 1, 1);
number.SetValue(363.31, 1, 1, 2);
number.SetValue(172.62, 1, 1, 3);
number.SetValue(128.48, 1, 1, 4);
number.SetValue(906.68, 1, 2, 0);
number.SetValue(47.12, 1, 2, 1);
number.SetValue(-166.07, 1, 2, 2);
number.SetValue(4444.26, 1, 2, 3);
number.SetValue(408.62, 1, 2, 4);

Console.WriteLine("Number: " + number.GetValue(0, 2, 4));
Console.WriteLine("===================================");
}
}```

This would produce:

```Number: 386.73
===================================
Press any key to continue . . .```

To access each member of the array, you can use three for loops. Here is an example:

```using System;

public class Exercise
{
public static void Main()
{
var number = Array.CreateInstance(typeof(double), 2, 3, 5);

number.SetValue(12.44, 0, 0, 0);
number.SetValue(525.38, 0, 0, 1);
number.SetValue(-6.28, 0, 0, 2);
number.SetValue(2448.32, 0, 0, 3);
number.SetValue(632.04, 0, 0, 4);
number.SetValue(-378.05, 0, 1, 0);
number.SetValue(48.14, 0, 1, 1);
number.SetValue(634.18, 0, 1, 2);
number.SetValue(762.48, 0, 1, 3);
number.SetValue(83.02, 0, 1, 4);
number.SetValue(64.92, 0, 2, 0);
number.SetValue(-7.44, 0, 2, 1);
number.SetValue(86.74, 0, 2, 2);
number.SetValue(-534.60, 0, 2, 3);
number.SetValue(386.73, 0, 2, 4);
number.SetValue(48.02, 1, 0, 0);
number.SetValue(120.44, 1, 0, 1);
number.SetValue(38.62, 1, 0, 2);
number.SetValue(526.82, 1, 0, 3);
number.SetValue(1704.62, 1, 0, 4);
number.SetValue(56.85, 1, 1, 0);
number.SetValue(105.48, 1, 1, 1);
number.SetValue(363.31, 1, 1, 2);
number.SetValue(172.62, 1, 1, 3);
number.SetValue(128.48, 1, 1, 4);
number.SetValue(906.68, 1, 2, 0);
number.SetValue(47.12, 1, 2, 1);
number.SetValue(-166.07, 1, 2, 2);
number.SetValue(4444.26, 1, 2, 3);
number.SetValue(408.62, 1, 2, 4);

for (int m = 0; m < number.GetLength(0); m++)
{
for (int n = 0; n < number.GetLength(1); n++)
{
for (int element = 0; element < number.GetLength(2); element++)
{
Console.WriteLine("Number: " + number.GetValue(m, n, element));
}
}
}

Console.WriteLine("===================================");
}
}```

This would produce:

```Number: 12.44
Number: 525.38
Number: -6.28
Number: 2448.32
Number: 632.04
Number: -378.05
Number: 48.14
Number: 634.18
Number: 762.48
Number: 83.02
Number: 64.92
Number: -7.44
Number: 86.74
Number: -534.6
Number: 386.73
Number: 48.02
Number: 120.44
Number: 38.62
Number: 526.82
Number: 1704.62
Number: 56.85
Number: 105.48
Number: 363.31
Number: 172.62
Number: 128.48
Number: 906.68
Number: 47.12
Number: -166.07
Number: 4444.26
Number: 408.62
===================================
Press any key to continue . . .```

You can also use a foreach loop to access each member of the array. Here is an example:

```using System;

public class Exercise
{
public static void Main()
{
var numbers = Array.CreateInstance(typeof(double), 2, 3, 5);

numbers.SetValue(12.44, 0, 0, 0);
numbers.SetValue(525.38, 0, 0, 1);
numbers.SetValue(-6.28, 0, 0, 2);
numbers.SetValue(2448.32, 0, 0, 3);
numbers.SetValue(632.04, 0, 0, 4);
numbers.SetValue(-378.05, 0, 1, 0);
numbers.SetValue(48.14, 0, 1, 1);
numbers.SetValue(634.18, 0, 1, 2);
numbers.SetValue(762.48, 0, 1, 3);
numbers.SetValue(83.02, 0, 1, 4);
numbers.SetValue(64.92, 0, 2, 0);
numbers.SetValue(-7.44, 0, 2, 1);
numbers.SetValue(86.74, 0, 2, 2);
numbers.SetValue(-534.60, 0, 2, 3);
numbers.SetValue(386.73, 0, 2, 4);
numbers.SetValue(48.02, 1, 0, 0);
numbers.SetValue(120.44, 1, 0, 1);
numbers.SetValue(38.62, 1, 0, 2);
numbers.SetValue(526.82, 1, 0, 3);
numbers.SetValue(1704.62, 1, 0, 4);
numbers.SetValue(56.85, 1, 1, 0);
numbers.SetValue(105.48, 1, 1, 1);
numbers.SetValue(363.31, 1, 1, 2);
numbers.SetValue(172.62, 1, 1, 3);
numbers.SetValue(128.48, 1, 1, 4);
numbers.SetValue(906.68, 1, 2, 0);
numbers.SetValue(47.12, 1, 2, 1);
numbers.SetValue(-166.07, 1, 2, 2);
numbers.SetValue(4444.26, 1, 2, 3);
numbers.SetValue(408.62, 1, 2, 4);

foreach (double number in numbers)
{
Console.WriteLine("Number: " + number);
}

Console.WriteLine("===================================");
}
}```

Multidimensional Arrays

The Array class supports all dimensions of arrays beyond three. To create a multidimensional array, the class is equipped with the following version of its CreateInstance() method:

`public static Array CreateInstance(Type elementType, params int[] lengths)`

To add elements to the list, you can use the following equivalent version of the SetValue() method:

`public void SetValue(object value, params int[] indices)`

To get the value of an element, you would call the following version of the GetValue() method:

`public Object GetValue(params int[] indices)`

Getting Other Information About an Array

The Lower Bound of an Array

To better manage an array, the compiler must always be able to locate its highest and its lowest members. This is particularly important because an array must have a size.

The lowest member of an array can be located using the Array.GetLowerBound() method. Its syntax is:

`public int GetLowerBound(int dimension);`

The Upper Bound of an Array

The highest member of an array can be located using the Array.GetUpperBound() method. Its syntax is:

`public int GetUpperBound(int dimension);`

In both cases, the dimension argument is the rank of the array. For a single-dimensional array, as those we have always used so far, this parameter must have the value of 0.

Practical Learning: Ending the Lesson