-
Key Features and
Create a new project in Visual Studio 2022 - Naming Standards & Comments
- Data Types
- Variables and Constant Variable
- Conditional Statement
- Switch Conditional Statement
- Arithmetic, Comparison, Assignment and Logical Operators
- For, While and Do While Loops
- Importance of Break and Continue Keywords
- Strings and Important String Methods
- Single Dimensional Array
- Multi Dimensional Array
- Methods and Method Overloading
- Math Class and Functions
- Casting and Type Conversions
- Class and Objects
- Constructor, Parameter less and Parameterized Constructor
- Single, Multi level and Hierarchical Inheritance
- Compile Time Polymorphism, Method and Operator Overloading
- Run-time Polymorphism and Importance of base keyword
- Abstract Class and Method
- Interface and Multiple Inheritance through Interfaces
Agenda
- Multi-Dimensional Array
- Update Multi-Dimensional Array Element Values
- Display Multi-Dimensional Array Elements in Console using for/foreach loop
Multi-Dimensional Array in C#
- C# supports multidimensional arrays up to 32 dimensions. The multidimensional array can be declared by adding commas in the square brackets. For example, [,] declares two-dimensional array, [, ,] declares three-dimensional array, [, , ,] declares four-dimensional array, and so on. So, in a multidimensional array, no of commas = No of Dimensions - 1.
- The multidimensional array is also known as rectangular arrays in C#.
- The following examples declares multidimensional arrays.
int[,] array2d; // two-dimensional array
int[, ,] array3d; // three-dimensional array
int[, , ,] array4d ; // four-dimensional array
- A 2-dimensional array can be thought of as a table, which has x number of rows and y number of columns. Two Dimensional Array Examples: int [,] array2 = {{1, 3, 5}, {2, 4, 6}}; int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 } }; string[,] array2Ds = new string[3, 2] { { “One", “Two" }, { “Three", “Four" }, { “Five", “Six" } };
- Accessing any element in the array: Console.WriteLine(array2[0,1]);
- User can update any element by updating the element value: array2[1, 1] = 12;
- Length of the array can be found using: Console.WriteLine(intArray.Length);
Multi-Dimensional Array in C# - Contd.
- For loop can be used to access the elements in multi-dimensional array. for (int i = 0; i < array2.GetLength(0); i++) { Console.Write("Row " + i + ": "); for (int j = 0; j < array2.GetLength(1); j++) { Console.Write(array2[i, j] + " "); } Console.WriteLine(); }
- Foreach loop can be used exclusively to loop through elements in multi-dimensional array. foreach (int i in array2) { Console.WriteLine("{0} ", i); }
Multi-Dimensional Array in C# - Contd.
int[,] arr3R3C = new int[3, 3];//declaration of 2D array
arr3R3C[0, 1] = 10;//initialization
arr3R3C[1, 2] = 20;
arr3R3C[2, 0] = 30;
arr3R3C[2, 1] = 40;
arr3R3C[2, 2] = 50;
//traversal
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(arr3R3C[i, j] + ” “); //undefined elements will take the
default value of data type, for int default value will be 0
}
Console.WriteLine();//new line at each row
}
int[,] intArray = { { 1, 2, 3 }, { 4, 5, 6 } };
intArray[0, 1] = 12;
Console.WriteLine(intArray[0,1]);
foreach (int i in intArray)
{
Console.WriteLine(i);
}
int[,] arr3R3C = new int[3, 3];//declaration of 2D array
arr3R3C[0, 1] = 10;//initialization
arr3R3C[1, 2] = 20;
arr3R3C[2, 0] = 30;
arr3R3C[2, 1] = 40;
arr3R3C[2, 2] = 50;
//traversal
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(arr3R3C[i, j] + " "); //undefined elements will take the default value of data type, for int it will be 0
}Console.WriteLine();//new line at each row
}
string[,] array2Db = new string[3, 2] { { "one", "two" }, { "three", "four" }, { "five", "six" } };
foreach (string s in array2Db)
{
System.Console.Write("{0} ", s);
}
var allArrayLength = intArray.Length;
var total = 1;
for (int i = 0; i < intArray.Rank; i++)
{
total *= intArray.GetLength(i);
}System.Console.WriteLine("{0} equals {1}", allArrayLength, total);
for (int i = 0; i < intArray.GetLength(0); i++)
{
Console.Write("Row " + i + ": ");
for (int j = 0; j < intArray.GetLength(1); j++)
{
Console.Write(intArray[i, j] + " ");
}
Console.WriteLine();
}
int[,] array2D = new int[,] { { 10, 2 }, { 3, 4 }, { 5, 7 } };
foreach (int i in array2D)
{
System.Console.Write("{0} ", i);
}