mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2025-12-06 05:32:03 +01:00
71 lines
1.9 KiB
C#
71 lines
1.9 KiB
C#
string[] names; // can reference any size array of strings
|
|
|
|
// allocating memory for four strings in an array
|
|
names = new string[4];
|
|
|
|
// storing items at index positions
|
|
names[0] = "Kate";
|
|
names[1] = "Jack";
|
|
names[2] = "Rebecca";
|
|
names[3] = "Tom";
|
|
|
|
string[] names2 = new[] { "Kate", "Jack", "Rebecca", "Tom" };
|
|
|
|
// looping through the names
|
|
for (int i = 0; i < names2.Length; i++)
|
|
{
|
|
// output the item at index position i
|
|
Console.WriteLine(names2[i]);
|
|
}
|
|
|
|
string[,] grid1 = new[,] // two dimensions
|
|
{
|
|
{ "Alpha", "Beta", "Gamma", "Delta" },
|
|
{ "Anne", "Ben", "Charlie", "Doug" },
|
|
{ "Aardvark", "Bear", "Cat", "Dog" }
|
|
};
|
|
|
|
Console.WriteLine($"Lower bound of the first dimension is: {grid1.GetLowerBound(0)}");
|
|
Console.WriteLine($"Upper bound of the first dimension is: {grid1.GetUpperBound(0)}");
|
|
Console.WriteLine($"Lower bound of the second dimension is: {grid1.GetLowerBound(1)}");
|
|
Console.WriteLine($"Upper bound of the second dimension is: {grid1.GetUpperBound(1)}");
|
|
|
|
for (int row = 0; row <= grid1.GetUpperBound(0); row++)
|
|
{
|
|
for (int col = 0; col <= grid1.GetUpperBound(1); col++)
|
|
{
|
|
Console.WriteLine($"Row {row}, Column {col}: {grid1[row, col]}");
|
|
}
|
|
}
|
|
|
|
// alternative syntax
|
|
string[,] grid2 = new string[3,4]; // allocate memory
|
|
grid2[0, 0] = "Alpha"; // assign string values
|
|
// and so on
|
|
grid2[2, 3] = "Dog";
|
|
|
|
string[][] jagged = new[] // array of string arrays
|
|
{
|
|
new[] { "Alpha", "Beta", "Gamma" },
|
|
new[] { "Anne", "Ben", "Charlie", "Doug" },
|
|
new[] { "Aardvark", "Bear" }
|
|
};
|
|
|
|
Console.WriteLine("Upper bound of array of arrays is: {0}",
|
|
jagged.GetUpperBound(0));
|
|
|
|
for (int array = 0; array <= jagged.GetUpperBound(0); array++)
|
|
{
|
|
Console.WriteLine("Upper bound of array {0} is: {1}",
|
|
arg0: array,
|
|
arg1: jagged[array].GetUpperBound(0));
|
|
}
|
|
|
|
for (int row = 0; row <= jagged.GetUpperBound(0); row++)
|
|
{
|
|
for (int col = 0; col <= jagged[row].GetUpperBound(0); col++)
|
|
{
|
|
Console.WriteLine($"Row {row}, Column {col}: {jagged[row][col]}");
|
|
}
|
|
}
|