Introduction to the Array Class
Introduction to the Array Class
Introduction to the Class to Manage Arrays
Overview
To use and manage arrays, you can combine the array features of the C# language and the support from the .NET Framework. As mentioned already, to support arrays, the .NET Framework provides the Array class that is defined in the System namespace of the mscorlib.dll assembly.
When you create an array in C# code, whether an array of primitive values or an array of objects, you are in fact declaring a variable of type Array. Based on this, since an array variable is an object of a class type, you can use the characteristics of the Array class to create an array and/or to manipulate the values stored in the variable. You can create an array using any of the techniques we saw in the previous lessons, or you can use the Array class.
Creating an Array
To assist you with creating an array, the Array class is equipped with a method named CreateInstance() that comes in various versions. To create a one-dimensional array whose members are zero-based, you can use the following version:
public static Array CreateInstance(Type elementType, int length);
The first argument is used to specify the type of array you want to create. You are allowed to create an array of any type, that is, an array of primitive values or an array of objects. The first argument is declared as Type. This means that it can be anything. For this reason, you can use the typeof operator to cast your type. Therefore, pass the type or the class, record, or structure in the parentheses of the typeof operator.
The second argument specifies the number of members of the array. Using the Array class, you can create an array as follows:
@{ Array numbers = Array.CreateInstance(typeof(double), length); } @functions{ public interface IAbbreviated { string? Abbreviation { get; init; } } public interface IGovernmentEntity { string? Name { get; init; } int Area { get; } string? Capital { get; init; } } public record Region { public string? Designation { get; init; } public string? Description { get; init; } } public class State : IGovernmentEntity, IAbbreviated { // From the IAbbreviated interface public string? Abbreviation { get; init; } // From the IGovernmentEntity interface public string? Name { get; init; } public int Area { get; init; } public string? Capital { get; init; } // New Properties public string? StateName => Name; public int AreaSqrMiles => Area; public int AreaSqrKms { get; init; } public Region Region { get; init; } = new Region(); } public class Federation : State { public int AdmissionUnionOrder { get; init; } } }
You can also use the var or the dynamic keyword to declare the variable. Here is an example:
var numbers = Array.CreateInstance(typeof(double), 5);
The Length of an Array
We know that if you declare a variable for an array but don't initialize it, you must specify the number of elements of the array. Here is an example:
@{
double[] numbers = new double[5];
}
If you use the Array class to create an array, you must pass this constant integer as the second argument of the CreateInstance() method from the above version. Here is an example:
@{
var numbers = Array.CreateInstance(typeof(double), 5);
}
If the array exists already, that is, if you have already created the array or you are using an array created by someone else, to find out the number of items it contains, you can access its Length property. Therefore, the length of an array is the number of elements it contains.
Alternatively, you can call the Array.GetLength() method. Its syntax is:
public int GetLength(int dimension);
For a one-dimensional array, you must pass the argument as 0. This method returns an integer that represents the number of items in the array.
The Rank of an Array
We have seen that the square brackets are used to specify that you are declaring an array. If you are creating a one-dimensional array, we saw that you could type a number in the square brackets. If you are creating a two-dimensional array, you type two numbers separated by a comma in the second pair of square brackets. Each number, whether it is one, two, or more is a placeholder for what is referred to as a dimension. In other words, a one-dimensional array has a dimension of one. A two-dimensional array has a dimension of 2, and so on.
To let you find out the dimension of an array, the Array class provides the Rank property. Therefore, to know the dimension of an existing array, you can access its Rank property.
Fundamental Operations on an Array
Adding a Value to an Array
We know that, to initialize an array, you can open the curly brackets and list its members separated by commas, or you could access each member and assign it the desired value. To support the ability to add members to an array, the Array class is equipped with a method named SetValue that comes in different versions. To add a new item to the types of arrays we have used so far, you can call the following version of the Array.SetValue() method:
public void SetValue(object value, int index);
The first argument is the value to add to the list. The second argument is the index of the member to be added. The first item has index 1; the second item has index 2, and so on. If the array is a list of primitive values, simply pass the value for the first argument. Here are examples of calling the method:
@{
Array numbers = Array.CreateInstance(typeof(double), 5);
numbers.SetValue(7628.937, 0);
numbers.SetValue(6.48, 1);
numbers.SetValue(574.9, 2);
numbers.SetValue(293749.064, 3);
numbers.SetValue(0.70257, 4);
}
If the array is a list of objects, use any technique to create an object and pass it as the first argument. Here are examples:
@{ Array elements = Array.CreateInstance(typeof(Element), 5); Element K = new Element(); K.Symbol = "K"; K.AtomicNumber = 19; K.ElementName = "Potassium"; K.AtomicWeight = 39.098f; Element Ca = new Element(20); Element Sc = new Element("Sc"); Element Ti = new Element(22, "Ti", "Titanium", 47.867F); elements.SetValue(K, 0); elements.SetValue(Ca, 1); elements.SetValue(Sc, 2); elements.SetValue(Ti, 3); elements.SetValue(new Element(23), 4); elements.SetValue(new Element(24, "Cr", "Chromium", 51.996F), 5); }
We indicated that whenever you create an array, you are in fact declaring an instance of the Array class. Therefore, even if you create an array using the square bracket formula, you can still call the SetValue() method to specify any member of the array. Here is an example that adds numeric values (values of primitive types) to an array:
@{ var numbers = new double[5]; numbers.SetValue(7628.937, 0); numbers.SetValue(6.48, 1); numbers.SetValue(574.9, 2); numbers.SetValue(293749.064, 3); numbers.SetValue(0.70257, 4); }
Here is an example that adds objects (of a class, a record, or structure) to an array:
@{ 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); } @functions{ public record Region { public string? Designation { get; init; } public string? Description { get; init; } } }
The Array class provides a SetValue() version for each corresponding CreateInstance() method we reviewed earlier.
Getting a Value from an Array
To support the ability to retrieve the value of a member of an array, the Array class is equipped with a method named GetValue that is overloaded with a version corresponding to each version of the CreateInstance() and the SetValue() methods. For example, to access the values stored in a one-dimensional array, you can call this version:
public object GetValue(int index);
The index argument is the zero-based index of the member whose value you want to access. If the array is a list of primitive values, the Array.GetValue() method directly produces the value stored in the indicated position. Here is an example:
@page
@model Exercises.Pages.ExerciseModel
@{
var numbers = new double[5];
numbers.SetValue(7628.937, 0);
numbers.SetValue(6.48, 1);
numbers.SetValue(574.9, 2);
numbers.SetValue(293749.064, 3);
numbers.SetValue(0.70257, 4);
}
<pre>Number: @numbers.GetValue(0)
=================================</pre>
This would produce:
Number: 7628.937 =================================
Notice that the Array.GetValue() method returns an anonymous object. This means that it can return anything. Therefore, if the array you are using is a list of objects, you must cast the returned object to the appropriate class. Here are examples:
@{ Array regions = Array.CreateInstance(typeof(Region), 9); Array states = Array.CreateInstance(typeof(Federation), 30); 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); states.SetValue(new Federation() { Abbreviation = "RI", Name = "Rhode Island", Area = 1545, AreaSqrKms = 4002, AdmissionUnionOrder = 13, Capital = "Providence", Region = (Region)(Regions.GetValue(2)) }, 0); states.SetValue(new Federation() { Abbreviation = "OH", Name = "Ohio", Area = 44828, AreaSqrKms = 116103, AdmissionUnionOrder = 17, Capital = "Columbus", Region = (Region)(Regions.GetValue(0)) }, 1); states.SetValue(new Federation() { Abbreviation = "KY", Name = "Kentucky", Area = 40411, AreaSqrKms = 104665, AdmissionUnionOrder = 15, Capital = "Frankfort", Region = (Region)(Regions.GetValue(1)) }, 2); }
When calling the Array.GetValue() method, if you pass an invalid value, the application would throw an exception.
Looping for an Array
To access one member of the array at a time, you can use a loop to access any member using its index. You can use any of the classic three loops: for, while, or do...while. Here is an example that uses a for loop and the Length property to know the number of members of an array:
@page @model Exercises.Pages.ExerciseModel @using static System.Console @{ const int length = 5; var numbers = new double[length]; numbers.SetValue(7628.937, 0); numbers.SetValue(6.48, 1); numbers.SetValue(574.9, 2); numbers.SetValue(293749.064, 3); numbers.SetValue(0.70257, 4); for(int i = 0; i < numbers.Length; i++) WriteLine("Number: {0}", numbers.GetValue(i)); WriteLine("================================="); }
This would produce:
Number: 7628.937 Number: 6.48 Number: 574.9 Number: 293749.064 Number: 0.70257 =================================
The above example dealt with regular values (values or primitive types). Here are examples of adding objects to an array:
@page
@model Valuable.Pages.ArraysModel
@using static System.Console;
@using static System.Environment;
@{
Array regions = Array.CreateInstance(typeof(Region), 9);
Array states = Array.CreateInstance(typeof(Federation), 30);
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, AdmissionUnionOrder = 13, Capital = "Providence ", Region = (Region)(regions?.GetValue(2)!) }, 0);
states.SetValue(new Federation() { Abbreviation = "OH", Name = "Ohio ", Area = 44828, AreaSqrKms = 116103, AdmissionUnionOrder = 17, Capital = "Columbus ", Region = (Region)(regions?.GetValue(0)!) }, 1);
states.SetValue(new Federation() { Abbreviation = "KY", Name = "Kentucky ", Area = 40411, AreaSqrKms = 104665, AdmissionUnionOrder = 15, Capital = "Frankfort ", Region = (Region)(regions?.GetValue(1)!) }, 2);
states.SetValue(new Federation() { Abbreviation = "IA", Name = "Iowa ", Area = 56276, AreaSqrKms = 145754, AdmissionUnionOrder = 29, Capital = "Des Moines ", Region = (Region)(regions?.GetValue(7)!) }, 3);
states.SetValue(new Federation() { Abbreviation = "WI", Name = "Wisconsin ", Area = 65503, AreaSqrKms = 169653, AdmissionUnionOrder = 30, Capital = "Madison ", Region = (Region)(regions?.GetValue(0)!) }, 4);
states.SetValue(new Federation() { Abbreviation = "VT", Name = "Vermont ", Area = 9615, AreaSqrKms = 24903, AdmissionUnionOrder = 14, Capital = "Montpelier ", Region = (Region)(regions?.GetValue(2)!) }, 5);
states.SetValue(new Federation() { Abbreviation = "ID", Name = "Idaho ", Area = 83574, AreaSqrKms = 216456, AdmissionUnionOrder = 43, Capital = "Boise ", Region = (Region)(regions?.GetValue(4)!) }, 6);
states.SetValue(new Federation() { Abbreviation = "ME", Name = "Maine ", Area = 35387, AreaSqrKms = 91653, AdmissionUnionOrder = 23, Capital = "Augusta ", Region = (Region)(regions?.GetValue(2)!) }, 7);
states.SetValue(new Federation() { Abbreviation = "OR", Name = "Oregon ", Area = 98386, AreaSqrKms = 254819, AdmissionUnionOrder = 33, Capital = "Salem ", Region = (Region)(regions?.GetValue(5)!) }, 8);
states.SetValue(new Federation() { Abbreviation = "ND", Name = "North Dakota ", Area = 70704, AreaSqrKms = 183123, AdmissionUnionOrder = 39, Capital = "Bismarck ", Region = (Region)(regions?.GetValue(7)!) }, 9);
states.SetValue(new Federation() { Abbreviation = "IN", Name = "Indiana ", Area = 36420, AreaSqrKms = 94328, AdmissionUnionOrder = 19, Capital = "Indianapolis ", Region = (Region)(regions?.GetValue(0)!) }, 10);
states.SetValue(new Federation() { Abbreviation = "MS", Name = "Mississippi ", Area = 48434, AreaSqrKms = 125443, AdmissionUnionOrder = 20, Capital = "Jackson ", Region = (Region)(regions?.GetValue(1)!) }, 11);
states.SetValue(new Federation() { Abbreviation = "TX", Name = "Texas ", Area = 268601, AreaSqrKms = 695676, AdmissionUnionOrder = 28, Capital = "Austin ", Region = (Region)(regions?.GetValue(8)!) }, 12);
states.SetValue(new Federation() { Abbreviation = "MT", Name = "Montana ", Area = 147046, AreaSqrKms = 380850, AdmissionUnionOrder = 41, Capital = "Helena ", Region = (Region)(regions?.GetValue(4)!) }, 13);
states.SetValue(new Federation() { Abbreviation = "NC", Name = "North Carolina", Area = 53821, AreaSqrKms = 139397, AdmissionUnionOrder = 12, Capital = "Raleigh ", Region = (Region)(regions?.GetValue(6)!) }, 14);
states.SetValue(new Federation() { Abbreviation = "TN", Name = "Tennessee ", Area = 42146, AreaSqrKms = 109158, AdmissionUnionOrder = 16, Capital = "Nashville ", Region = (Region)(regions?.GetValue(1)!) }, 15);
states.SetValue(new Federation() { Abbreviation = "NE", Name = "Nebraska ", Area = 77358, AreaSqrKms = 200358, AdmissionUnionOrder = 37, Capital = "Lincoln ", Region = (Region)(regions?.GetValue(7)!) }, 16);
states.SetValue(new Federation() { Abbreviation = "IL", Name = "Illinois ", Area = 57918, AreaSqrKms = 150007, AdmissionUnionOrder = 21, Capital = "Springfield ", Region = (Region)(regions?.GetValue(0)!) }, 17);
states.SetValue(new Federation() { Abbreviation = "KS", Name = "Kansas ", Area = 82282, AreaSqrKms = 213110, AdmissionUnionOrder = 34, Capital = "Topeka ", Region = (Region)(regions?.GetValue(7)!) }, 18);
states.SetValue(new Federation() { Abbreviation = "NH", Name = "New Hampshire ", Area = 9351, AreaSqrKms = 24219, AdmissionUnionOrder = 9, Capital = "Concord ", Region = (Region)(regions?.GetValue(2)!) }, 19);
states.SetValue(new Federation() { Abbreviation = "DE", Name = "Delaware ", Area = 2489, AreaSqrKms = 6447, AdmissionUnionOrder = 1, Capital = "Dover ", Region = (Region)(regions?.GetValue(6)!) }, 20);
states.SetValue(new Federation() { Abbreviation = "NJ", Name = "New Jersey ", Area = 8722, AreaSqrKms = 22590, AdmissionUnionOrder = 3, Capital = "Trenton ", Region = (Region)(regions?.GetValue(3)!) }, 21);
states.SetValue(new Federation() { Abbreviation = "AK", Name = "Alaska ", Area = 656424, AreaSqrKms = 1700139, AdmissionUnionOrder = 49, Capital = "Juneau ", Region = (Region)(regions?.GetValue(5)!) }, 22);
states.SetValue(new Federation() { Abbreviation = "NM", Name = "New Mexico ", Area = 121598, AreaSqrKms = 314939, AdmissionUnionOrder = 47, Capital = "Santa Fe ", Region = (Region)(regions?.GetValue(4)!) }, 23);
states.SetValue(new Federation() { Abbreviation = "NY", Name = "New York ", Area = 54475, AreaSqrKms = 141089, AdmissionUnionOrder = 11, Capital = "Albany ", Region = (Region)(regions?.GetValue(3)!) }, 24);
states.SetValue(new Federation() { Abbreviation = "CA", Name = "California ", Area = 163707, AreaSqrKms = 424002, AdmissionUnionOrder = 31, Capital = "Sacramento ", Region = (Region)(regions?.GetValue(5)!) }, 25);
states.SetValue(new Federation() { Abbreviation = "MO", Name = "Missouri ", Area = 69709, AreaSqrKms = 180546, AdmissionUnionOrder = 24, Capital = "Jefferson City", Region = (Region)(regions?.GetValue(7)!) }, 26);
states.SetValue(new Federation() { Abbreviation = "OK", Name = "Oklahoma ", Area = 69903, AreaSqrKms = 181049, AdmissionUnionOrder = 46, Capital = "Oklahoma City ", Region = (Region)(regions?.GetValue(8)!) }, 27);
states.SetValue(new Federation() { Abbreviation = "PA", Name = "Pennsylvania ", Area = 46058, AreaSqrKms = 119291, AdmissionUnionOrder = 2, Capital = "Harrisburg ", Region = (Region)(regions?.GetValue(3)!) }, 28);
states.SetValue(new Federation() { Abbreviation = "SC", Name = "South Carolina", Area = 32007, AreaSqrKms = 82898, AdmissionUnionOrder = 8, Capital = "Columbia ", Region = (Region)(regions?.GetValue(6)!) }, 29);
WriteLine("================================================================================================================");
WriteLine(" United States of America");
WriteLine("================================================================================================================");
WriteLine(" # Area Area Adm to Federation");
WriteLine(" # Abbrv State Name (Sqr Kms) (Sqr Miles) (Order) Capital Region");
WriteLine("================================================================================================================");
for(int counter = 0; counter <= states?.Length - 1; counter++)
{
Federation stt = (Federation)states?.GetValue(counter)!;
WriteLine($" {(counter + 1),2} {stt.Abbreviation} {stt.StateName} {stt.AreaSqrMiles,8} {stt.AreaSqrKms,15} {stt.AdmissionUnionOrder,8} {stt.Capital} {stt.Region.Designation}");
WriteLine("----------------------------------------------------------------------------------------------------------------");
}
}
@functions{
public record Region
{
public string? Designation { get; init; }
public string? Description { get; init; }
}
public interface IAbbreviated
{
string? Abbreviation { get; init; }
}
public interface IGovernmentEntity
{
string? Name { get; init; }
int Area { get; }
string? Capital { get; init; }
}
public class State : IGovernmentEntity, IAbbreviated
{
// From the IAbbreviated interface
public string? Abbreviation { get; init; }
// From the IGovernmentEntity interface
public string? Name { get; init; }
public int Area { get; init; }
public string? Capital { get; init; }
// New Properties
public string? StateName => Name;
public int AreaSqrMiles => Area;
public int AreaSqrKms { get; init; }
public Region Region { get; init; } = new Region();
}
public class Federation : State
{
public int AdmissionUnionOrder { get; init; }
}
}
This would produce:
================================================================================================================ United States of America ================================================================================================================ # Area Area Adm to Federation # Abbrv State Name (Sqr Kms) (Sqr Miles) (Order) Capital Region ================================================================================================================ 1 RI Rhode Island 1545 4002 13 Providence New England ---------------------------------------------------------------------------------------------------------------- 2 OH Ohio 44828 116103 17 Columbus East North Central ---------------------------------------------------------------------------------------------------------------- 3 KY Kentucky 40411 104665 15 Frankfort East South Central ---------------------------------------------------------------------------------------------------------------- 4 IA Iowa 56276 145754 29 Des Moines West North Central ---------------------------------------------------------------------------------------------------------------- 5 WI Wisconsin 65503 169653 30 Madison East North Central ---------------------------------------------------------------------------------------------------------------- 6 VT Vermont 9615 24903 14 Montpelier New England ---------------------------------------------------------------------------------------------------------------- 7 ID Idaho 83574 216456 43 Boise Mountain ---------------------------------------------------------------------------------------------------------------- 8 ME Maine 35387 91653 23 Augusta New England ---------------------------------------------------------------------------------------------------------------- 9 OR Oregon 98386 254819 33 Salem Pacific ---------------------------------------------------------------------------------------------------------------- 10 ND North Dakota 70704 183123 39 Bismarck West North Central ---------------------------------------------------------------------------------------------------------------- 11 IN Indiana 36420 94328 19 Indianapolis East North Central ---------------------------------------------------------------------------------------------------------------- 12 MS Mississippi 48434 125443 20 Jackson East South Central ---------------------------------------------------------------------------------------------------------------- 13 TX Texas 268601 695676 28 Austin West South Central ---------------------------------------------------------------------------------------------------------------- 14 MT Montana 147046 380850 41 Helena Mountain ---------------------------------------------------------------------------------------------------------------- 15 NC North Carolina 53821 139397 12 Raleigh South Atlantic ---------------------------------------------------------------------------------------------------------------- 16 TN Tennessee 42146 109158 16 Nashville East South Central ---------------------------------------------------------------------------------------------------------------- 17 NE Nebraska 77358 200358 37 Lincoln West North Central ---------------------------------------------------------------------------------------------------------------- 18 IL Illinois 57918 150007 21 Springfield East North Central ---------------------------------------------------------------------------------------------------------------- 19 KS Kansas 82282 213110 34 Topeka West North Central ---------------------------------------------------------------------------------------------------------------- 20 NH New Hampshire 9351 24219 9 Concord New England ---------------------------------------------------------------------------------------------------------------- 21 DE Delaware 2489 6447 1 Dover South Atlantic ---------------------------------------------------------------------------------------------------------------- 22 NJ New Jersey 8722 22590 3 Trenton Mid-Atlantic ---------------------------------------------------------------------------------------------------------------- 23 AK Alaska 656424 1700139 49 Juneau Pacific ---------------------------------------------------------------------------------------------------------------- 24 NM New Mexico 121598 314939 47 Santa Fe Mountain ---------------------------------------------------------------------------------------------------------------- 25 NY New York 54475 141089 11 Albany Mid-Atlantic ---------------------------------------------------------------------------------------------------------------- 26 CA California 163707 424002 31 Sacramento Pacific ---------------------------------------------------------------------------------------------------------------- 27 MO Missouri 69709 180546 24 Jefferson City West North Central ---------------------------------------------------------------------------------------------------------------- 28 OK Oklahoma 69903 181049 46 Oklahoma City West South Central ---------------------------------------------------------------------------------------------------------------- 29 PA Pennsylvania 46058 119291 2 Harrisburg Mid-Atlantic ---------------------------------------------------------------------------------------------------------------- 30 SC South Carolina 32007 82898 8 Columbia South Atlantic ----------------------------------------------------------------------------------------------------------------
We have seen that you can create an array using the its Array class and its members (methods and properties) such as the SetValue() method used to populate an array. Otherwise, you can use the array using the classic techniques. Here are examples:
@page @model Valuable.Pages.ArraysModel @using static System.Console; @using static System.Environment; @{ Federation[] states = new Federation[30]; Region[] regions = new Region[9]; regions[0] = new Region() { Designation = "East North Central", Description = "The East North Central region includes the states around the Great Lakes." }; regions[1] = new Region() { Designation = "East South Central", Description = "The East South Central portion is one of the regions designated as the South." }; regions[2] = 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." }; regions[3] = 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." }; regions[4] = 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." }; regions[5] = 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." }; regions[6] = 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." }; regions[7] = 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." }; regions[8] = 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." }; states[0] = new Federation() { Abbreviation = "RI", Name = "Rhode Island ", Area = 1545, AreaSqrKms = 4002, AdmissionUnionOrder = 13, Capital = "Providence ", Region = (Region)(regions?.GetValue(2)!) }; states[1] = new Federation() { Abbreviation = "OH", Name = "Ohio ", Area = 44828, AreaSqrKms = 116103, AdmissionUnionOrder = 17, Capital = "Columbus ", Region = (Region)(regions?.GetValue(0)!) }; states[2] = new Federation() { Abbreviation = "KY", Name = "Kentucky ", Area = 40411, AreaSqrKms = 104665, AdmissionUnionOrder = 15, Capital = "Frankfort ", Region = (Region)(regions?.GetValue(1)!) }; states[3] = new Federation() { Abbreviation = "IA", Name = "Iowa ", Area = 56276, AreaSqrKms = 145754, AdmissionUnionOrder = 29, Capital = "Des Moines ", Region = (Region)(regions?.GetValue(7)!) }; states[4] = new Federation() { Abbreviation = "WI", Name = "Wisconsin ", Area = 65503, AreaSqrKms = 169653, AdmissionUnionOrder = 30, Capital = "Madison ", Region = (Region)(regions?.GetValue(0)!) }; states[5] = new Federation() { Abbreviation = "VT", Name = "Vermont ", Area = 9615, AreaSqrKms = 24903, AdmissionUnionOrder = 14, Capital = "Montpelier ", Region = (Region)(regions?.GetValue(2)!) }; states[6] = new Federation() { Abbreviation = "ID", Name = "Idaho ", Area = 83574, AreaSqrKms = 216456, AdmissionUnionOrder = 43, Capital = "Boise ", Region = (Region)(regions?.GetValue(4)!) }; states[7] = new Federation() { Abbreviation = "ME", Name = "Maine ", Area = 35387, AreaSqrKms = 91653, AdmissionUnionOrder = 23, Capital = "Augusta ", Region = (Region)(regions?.GetValue(2)!) }; states[8] = new Federation() { Abbreviation = "OR", Name = "Oregon ", Area = 98386, AreaSqrKms = 254819, AdmissionUnionOrder = 33, Capital = "Salem ", Region = (Region)(regions?.GetValue(5)!) }; states[9] = new Federation() { Abbreviation = "ND", Name = "North Dakota ", Area = 70704, AreaSqrKms = 183123, AdmissionUnionOrder = 39, Capital = "Bismarck ", Region = (Region)(regions?.GetValue(7)!) }; states[10] = new Federation() { Abbreviation = "IN", Name = "Indiana ", Area = 36420, AreaSqrKms = 94328, AdmissionUnionOrder = 19, Capital = "Indianapolis ", Region = (Region)(regions?.GetValue(0)!) }; states[11] = new Federation() { Abbreviation = "MS", Name = "Mississippi ", Area = 48434, AreaSqrKms = 125443, AdmissionUnionOrder = 20, Capital = "Jackson ", Region = (Region)(regions?.GetValue(1)!) }; states[12] = new Federation() { Abbreviation = "TX", Name = "Texas ", Area = 268601, AreaSqrKms = 695676, AdmissionUnionOrder = 28, Capital = "Austin ", Region = (Region)(regions?.GetValue(8)!) }; states[13] = new Federation() { Abbreviation = "MT", Name = "Montana ", Area = 147046, AreaSqrKms = 380850, AdmissionUnionOrder = 41, Capital = "Helena ", Region = (Region)(regions?.GetValue(4)!) }; states[14] = new Federation() { Abbreviation = "NC", Name = "North Carolina", Area = 53821, AreaSqrKms = 139397, AdmissionUnionOrder = 12, Capital = "Raleigh ", Region = (Region)(regions?.GetValue(6)!) }; states[15] = new Federation() { Abbreviation = "TN", Name = "Tennessee ", Area = 42146, AreaSqrKms = 109158, AdmissionUnionOrder = 16, Capital = "Nashville ", Region = (Region)(regions?.GetValue(1)!) }; states[16] = new Federation() { Abbreviation = "NE", Name = "Nebraska ", Area = 77358, AreaSqrKms = 200358, AdmissionUnionOrder = 37, Capital = "Lincoln ", Region = (Region)(regions?.GetValue(7)!) }; states[17] = new Federation() { Abbreviation = "IL", Name = "Illinois ", Area = 57918, AreaSqrKms = 150007, AdmissionUnionOrder = 21, Capital = "Springfield ", Region = (Region)(regions?.GetValue(0)!) }; states[18] = new Federation() { Abbreviation = "KS", Name = "Kansas ", Area = 82282, AreaSqrKms = 213110, AdmissionUnionOrder = 34, Capital = "Topeka ", Region = (Region)(regions?.GetValue(7)!) }; states[19] = new Federation() { Abbreviation = "NH", Name = "New Hampshire ", Area = 9351, AreaSqrKms = 24219, AdmissionUnionOrder = 9, Capital = "Concord ", Region = (Region)(regions?.GetValue(2)!) }; states[20] = new Federation() { Abbreviation = "DE", Name = "Delaware ", Area = 2489, AreaSqrKms = 6447, AdmissionUnionOrder = 1, Capital = "Dover ", Region = (Region)(regions?.GetValue(6)!) }; states[21] = new Federation() { Abbreviation = "NJ", Name = "New Jersey ", Area = 8722, AreaSqrKms = 22590, AdmissionUnionOrder = 3, Capital = "Trenton ", Region = (Region)(regions?.GetValue(3)!) }; states[22] = new Federation() { Abbreviation = "AK", Name = "Alaska ", Area = 656424, AreaSqrKms = 1700139, AdmissionUnionOrder = 49, Capital = "Juneau ", Region = (Region)(regions?.GetValue(5)!) }; states[23] = new Federation() { Abbreviation = "NM", Name = "New Mexico ", Area = 121598, AreaSqrKms = 314939, AdmissionUnionOrder = 47, Capital = "Santa Fe ", Region = (Region)(regions?.GetValue(4)!) }; states[24] = new Federation() { Abbreviation = "NY", Name = "New York ", Area = 54475, AreaSqrKms = 141089, AdmissionUnionOrder = 11, Capital = "Albany ", Region = (Region)(regions?.GetValue(3)!) }; states[25] = new Federation() { Abbreviation = "CA", Name = "California ", Area = 163707, AreaSqrKms = 424002, AdmissionUnionOrder = 31, Capital = "Sacramento ", Region = (Region)(regions?.GetValue(5)!) }; states[26] = new Federation() { Abbreviation = "MO", Name = "Missouri ", Area = 69709, AreaSqrKms = 180546, AdmissionUnionOrder = 24, Capital = "Jefferson City", Region = (Region)(regions?.GetValue(7)!) }; states[27] = new Federation() { Abbreviation = "OK", Name = "Oklahoma ", Area = 69903, AreaSqrKms = 181049, AdmissionUnionOrder = 46, Capital = "Oklahoma City ", Region = (Region)(regions?.GetValue(8)!) }; states[28] = new Federation() { Abbreviation = "PA", Name = "Pennsylvania ", Area = 46058, AreaSqrKms = 119291, AdmissionUnionOrder = 2, Capital = "Harrisburg ", Region = (Region)(regions?.GetValue(3)!) }; states[29] = new Federation() { Abbreviation = "SC", Name = "South Carolina", Area = 32007, AreaSqrKms = 82898, AdmissionUnionOrder = 8, Capital = "Columbia ", Region = (Region)(regions?.GetValue(6)!) }; WriteLine("================================================================================================================"); WriteLine(" United States of America"); WriteLine("================================================================================================================"); WriteLine(" # Area Area Adm to Federation"); WriteLine(" # Abbrv State Name (Sqr Kms) (Sqr Miles) (Order) Capital Region"); WriteLine("================================================================================================================"); for (int counter = 0; counter <= states?.Length - 1; counter++) { Federation stt = (Federation)states?.GetValue(counter)!; WriteLine($" {(counter + 1),2} {stt.Abbreviation} {stt.StateName} {stt.AreaSqrMiles,8} {stt.AreaSqrKms,15} {stt.AdmissionUnionOrder,8} {stt.Capital} {stt.Region.Designation}"); WriteLine("----------------------------------------------------------------------------------------------------------------"); } }
|
|||
Previous | Copyright © 2008-2022, FunctionX | Friday 18 February 2022 | Next |
|