Algorithm


 In C#, you can declare an array using the following syntax:

csharp
data_type[] array_name;
 

Here, data_type is the type of elements that the array will hold, and array_name is the name of the array. For example, if you want to declare an array of integers, you would use the int data type:

csharp
int[] myArray;
 

You can also initialize the array at the time of declaration by providing the initial values:

csharp
int[] myArray = { 1, 2, 3, 4, 5 };
 

Alternatively, you can use the new keyword to create an instance of the array with a specific size:

csharp
int[] myArray = new int[5];
 

This creates an integer array with a length of 5, and all elements are initialized to their default values (which is 0 for integers).

You can access elements in the array using their index, starting from 0. For example:

csharp
int value = myArray[2]; // Access the third element in the array (index 2)
 

Remember to be cautious with array indices to avoid accessing elements outside the bounds of the array, which can result in runtime errors.

 

Array initialization in C# Programming

  1. In C#, you can initialize an array in several ways:

    1. Inline Initialization: You can initialize an array with values at the time of declaration:

      csharp
      int[] myArray = { 1, 2, 3, 4, 5 };
  2.  

    This creates an array of integers with the specified values.

  3. Using the new Keyword: You can also initialize an array using the new keyword, specifying the length of the array:

    csharp
    int[] myArray = new int[5];
  4.  

    This creates an array of integers with a length of 5, and all elements are initialized to their default values (which is 0 for integers).

  5. Initializing with a Loop: You can initialize the array using a loop:

    csharp
    int[] myArray = new int[5];
    for (int i = 0; i < myArray.Length; i++)
    {
        myArray[i] = i + 1;
    }
  6.  

    This sets each element in the array to its index plus one.

  7. Multidimensional Arrays: For multidimensional arrays, you can use the following syntax:

    csharp
    int[,] twoDArray = { { 1, 2, 3 }, { 4, 5, 6 } };
  8.  

    This creates a 2D array with two rows and three columns.

  9. Jagged Arrays: Jagged arrays are arrays of arrays. You can initialize them like this:

    csharp
    int[][] jaggedArray = new int[3][];
    jaggedArray[0] = new int[] { 1, 2, 3 };
    jaggedArray[1] = new int[] { 4, 5, 6, 7 };
    jaggedArray[2] = new int[] { 8, 9 };
  10.  

    This creates a jagged array with three arrays of different lengths.

 

 

 

Code Examples

#1 Code Example- C# Programming Array

Code - C# Programming

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

    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Element in first index : 1
Element in second index : 2
Element in third index : 3

#2 Change Array Elements in C# programming

Code - C# Programming

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();
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Old Value at index 0: 1
New Value at index 0: 11

#3 Using for loop in C# Programming

Code - C# Programming

using System;

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

      int[] numbers = { 1, 2, 3};
 	 
      for(int i=0; i  <  numbers.Length; i++) {
        Console.WriteLine("Element in index " + i + ": " + numbers[i]);
      }

      Console.ReadLine();
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Element in index 0: 1
Element in index 1: 2
Element in index 2: 3

#4 Using foreach loop in C# Programming

Code - C# Programming

using System;

namespace AccessArrayForeach {
  class Program {
    static void Main(string[] args) {
      int[] numbers = {1, 2, 3};

      Console.WriteLine("Array Elements: ");

      foreach(int num in numbers) {
        Console.WriteLine(num);
      }

      Console.ReadLine();
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Array Elements:
1
2
3

#5 Find Minimum and Maximum Element in C# programming

Code - C# Programming

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();
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Smallest Element: 1
Largest Element: 98

#6 Find the Average of an Array In C# Programming

Code - C# Programming

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();
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Average : 59.2
Average using Average() : 59.2
Advertisements

Demonstration


C# Programming Arrays