0% found this document useful (0 votes)
13 views

Arrays Using C-sharp

The document provides an overview of arrays in C#, detailing their types, including single-dimensional, multidimensional, and jagged arrays. It includes examples of array declaration, initialization, traversal, and operations using LINQ, as well as how to pass arrays to functions. Additionally, it covers how to manipulate array elements and perform calculations such as finding minimum, maximum, and average values.

Uploaded by

Elineus Peter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Arrays Using C-sharp

The document provides an overview of arrays in C#, detailing their types, including single-dimensional, multidimensional, and jagged arrays. It includes examples of array declaration, initialization, traversal, and operations using LINQ, as well as how to pass arrays to functions. Additionally, it covers how to manipulate array elements and perform calculations such as finding minimum, maximum, and average values.

Uploaded by

Elineus Peter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

C# Arrays

Like other programming languages, array in C# is a group of similar types of


elements that have contiguous memory location. In C#, array is an object of
base type System. Array. In C#, array index starts from 0. We can store only
fixed set of elements in C# array.

C# Array Types
There are 3 types of arrays in C# programming:

1. Single Dimensional Array


2. Multidimensional Array
3. Jagged Array

C# Single Dimensional Array


To create single dimensional array, you need to use square brackets [] after the
type.

int[] arr = new int[5];//creating array


You cannot place square brackets after the identifier.

int arr[] = new int[5];//compile time error


Let's see a simple example of C# array, where we are going to declare, initialize
and traverse array.

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]);
}
}
}

C# Array Example: Declaration and Initialization at same


time
There are 3 ways to initialize array at the time of declaration.

int[] arr = new int[5]{ 10, 20, 30, 40, 50 };


We can omit the size of array.

int[] arr = new int[]{ 10, 20, 30, 40, 50 };


We can omit the new operator also.

int[] arr = { 10, 20, 30, 40, 50 };


Let's see the example of array where we are declaring and initializing array at
the same time.

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};

//access first element


Console.WriteLine("Element in first index : " + numbers[0]);

//access second element


Console.WriteLine("Element in second index : " + numbers[1]);

//access third element


Console.WriteLine("Element in third index : " + numbers[2]);
Console.ReadLine();

}
Output

Element in first index : 1


Element in second index : 2
Element in third index : 3

C# Array Example 3: Change Array Elements


using System;

namespace ChangeArray {
class Program {
static void Main(string[] args) {

// create an array
int[] numbers = {1, 2, 3};

Console.WriteLine("Old Value at index 0: " + numbers[0]);

// change the value at index 0


numbers[0] = 11;

//print new value


Console.WriteLine("New Value at index 0: " + numbers[0]);

Console.ReadLine();
}
}
}

Output

Old Value at index 0: 1


New Value at index 0: 11

In the above example, the initial value at index 0 is 1. Notice the line,

//change the value at index 0


numbers[0] = 11;

C# Array Example 4: Array Operations using System.Linq

In C#, we have the System.Linq namespace that provides different


methods to perform various operations in an array. For example,

Example: Find Minimum and Maximum Element

using System;

// provides us various methods to use in an array


using System.Linq;

namespace ArrayMinMax {
class Program {
static void Main(string[] args) {

int[] numbers = {51, 1, 3, 4, 98};

// get the minimum element


Console.WriteLine("Smallest Element: " + numbers.Min());

// Max() returns the largest number in array


Console.WriteLine("Largest Element: " + numbers.Max());

Console.ReadLine();
}
}
}

Output

Smallest Element: 1
Largest Element: 98

C# Array Example 5: Find the Average of an Array

using System;
// provides us various methods to use in an array
using System.Linq;

namespace ArrayFunction {
class Program {
static void Main(string[] args) {

int[] numbers = {30, 31, 94, 86, 55};

// get the sum of all array elements


float sum = numbers.Sum();

// get the total number of elements present in the array


int count = numbers.Count();

float average = sum/count;

Console.WriteLine("Average : " + average);

// compute the average


Console.WriteLine("Average using Average() : " + numbers.Average());

Console.ReadLine();
}
}
}

C# Passing Array to Function


In C#, to reuse the array logic, we can create function. To pass array to
function in C#, we need to provide only array name.

functionname(arrayname);//passing array

C# Passing Array to Function Example: print array elements


Let's see an example of C# function which prints the array elements.

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);
}
}

C# Passing Array to Function Example: Print minimum number

Let's see an example of C# array which prints minimum number in an


array using function.
using System;
public class ArrayExample
{
static void printMin(int[] arr)
{
int min = arr[0];
for (int i = 1; i < arr.Length; i++)
{
if (min > arr[i])
{
min = arr[i];
}

}
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 };

printMin(arr1);//passing array to function


printMin(arr2);
}
}

Output:
Minimum element is: 10
Minimum element is: 11

C# Passing Array to Function Example: Print maximum number


Let's see an example of C# array which prints maximum number in an
array using function.
using System;
public class ArrayExample
{
static void printMax(int[] arr)
{
int max = arr[0];

for (int i = 1; i < arr.Length; i++)


{
if (max < arr[i])
{
max = arr[i];
}
}
Console.WriteLine("Maximum element is: " + max);
}
public static void Main(string[] args)
{
int[] arr1 = { 25, 10, 20, 15, 40, 50 };
int[] arr2 = { 12, 23, 64, 11, 54 };

printMax(arr1);//passing array to function


printMax(arr2);
}
}

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.

To create multidimensional array, we need to use comma inside the square


brackets. For example:
1. int[,] arr=new int[3,3];//declaration of 2D array

2. int[,,] arr=new int[3,3,3];//declaration of 3D array

C# Multidimensional Array Example


Let's see a simple example of multidimensional array in C# which declares,
initializes and traverse two dimensional array.

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

Example: Declaration and initialization at same time


There are 3 ways to initialize multidimensional array in C# while declaration.
int[,] arr = new int[3,3]= { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
We can omit the array size.

int[,] arr = new int[,]{ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };


We can omit the new operator also.

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

You might also like