Arrays Using C-sharp
Arrays Using C-sharp
C# Array Types
There are 3 types of arrays in C# programming:
using System;
public class ArrayExample
{
public static void Main(string[] args)
{
int[] arr = new int[5];//creating array
arr[0] = 10;//initializing array
arr[2] = 20;
arr[4] = 30;
//traversing array
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i]);
}
}
}
using System;
public class ArrayExample
{
public static void Main(string[] args)
{
int[] arr = { 10, 20, 30, 40, 50 };//Declaration and Initialization of array
//traversing array
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i]);
}
}
}
C# Array Example 1:
We can also traverse the array elements using foreach loop. It returns array
element one by one.
using System;
public class ArrayExample
{
public static void Main(string[] args)
{
int[] arr = { 10, 20, 30, 40, 50 };//creating and initializing array
//traversing array
foreach (int i in arr)
{
Console.WriteLine(i);
}
}
}
C# Array Example 2:
using System;
namespace AccessArray {
class Program {
static void Main(string[] args) {
// create an array
int[] numbers = {1, 2, 3};
}
Output
namespace ChangeArray {
class Program {
static void Main(string[] args) {
// create an array
int[] numbers = {1, 2, 3};
Console.ReadLine();
}
}
}
Output
In the above example, the initial value at index 0 is 1. Notice the line,
using System;
namespace ArrayMinMax {
class Program {
static void Main(string[] args) {
Console.ReadLine();
}
}
}
Output
Smallest Element: 1
Largest Element: 98
using System;
// provides us various methods to use in an array
using System.Linq;
namespace ArrayFunction {
class Program {
static void Main(string[] args) {
Console.ReadLine();
}
}
}
functionname(arrayname);//passing array
using System;
public class ArrayExample
{
static void printArray(int[] arr)
{
Console.WriteLine("Printing array elements:");
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i]);
}
}
public static void Main(string[] args)
{
int[] arr1 = { 25, 10, 20, 15, 40, 50 };
int[] arr2 = { 12, 23, 44, 11, 54 };
printArray(arr1);//passing array to function
printArray(arr2);
}
}
}
Console.WriteLine("Minimum element is: " + min);
}
public static void Main(string[] args)
{
int[] arr1 = { 25, 10, 20, 15, 40, 50 };
int[] arr2 = { 12, 23, 44, 11, 54 };
Output:
Minimum element is: 10
Minimum element is: 11
C# Multidimensional Arrays
The multidimensional array is also known as rectangular arrays in C#. It can be
two dimensional or three dimensional. The data is stored in tabular form (row *
column) which is also known as matrix.
using System;
public class MultiArrayExample
{
public static void Main(string[] args)
{
int[,] arr=new int[3,3];//declaration of 2D array
arr[0,1]=10;//initialization
arr[1,2]=20;
arr[2,0]=30;
//traversal
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
Console.Write(arr[i,j]+" ");
}
Console.WriteLine();//new line at each row
}
}
}
Output:
0 10 0
0 0 20
30 0 0
C# Multidimensional Array
int[,] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
Let's see a simple example of multidimensional array which initializes array at
the time of declaration.
using System;
public class MultiArrayExample
{
public static void Main(string[] args)
{
int[,] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };//declaration and initialization
//traversal
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
Console.Write(arr[i,j]+" ");
}
Console.WriteLine();//new line at each row
}
}
}
Output:
123
456
789