Single Dimensional Arrays in C#

Learn: Single dimensional array in C#.Net, how to declare, read and print the elements of an array? By IncludeHelp Last updated : April 11, 2023

Introduction

Just like other programming languages, C# arrays are also used to hold similar type of data elements, they are user defined data types. Arrays are references in C#.

Single-dimensional Array Creation/Declaration

To create/declare a single-dimensional array, we use new operator specifying the type and the number of elements of array.

Syntax

<data_type>[] variable_name = new <data_type>[SIZE];

Consider the below statement to create single-dimensional array of 100 integer elements.

int[] arr = new int[100];

Here, new operator is used to allocate memory space for the array.

Single-dimensional Array Initialization

There are two ways to initialize array elements:

  1. During array declaration
  2. After array declaration

1. During array declaration

The following code shows an initialization of single-dimensional array during an array declaration.

int[] arr = new int[] { 1, 2, 3, 4, 5 };

2. After array declaration

The following code shows an initialization of single-dimensional array when an array is already declared.

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

Access Elements of a Single-dimensional Array

To access an individual element of an array, use the array_name followed by the index of the element in square brackets []. Where, array indices starts from 0 to size-1.

Syntax

array_name[index];

The following code shows accessing elements at index 0 and 2.

arr [0];
arr [2];

Single-dimensional Array Examples

Practice these programs to learn the concept of single-dimensional array in C#.

Example 1

Declare a single-dimensional array, input, and print array elements.

using System;

namespace arrayEx {
  class Program {
    static void Main(string[] args) {
      int i = 0;
      int[] arr;
      
      // Declaration
      arr = new int[5];
      
      // Reading array elements
      Console.Write("Enter Elements : \n");
      for (i = 0; i < 5; i++) {
        Console.Write("\tElement[" + i + "]: ");
        arr[i] = Convert.ToInt32(Console.ReadLine());
      }
      
      // Printing array elements
      Console.Write("\n\nElements are: \n");
      for (i = 0; i < 5; i++) {
        Console.WriteLine("\tElement[" + i + "]: " + arr[i]);
      }
    }
  }
}

Output

Enter Elements :
        Element[0]: 10
        Element[1]: 20
        Element[2]: 30
        Element[3]: 40
        Element[4]: 50
                      
Elements are:         
        Element[0]: 10
        Element[1]: 20
        Element[2]: 30
        Element[3]: 40
        Element[4]: 50    
Press any key to continue . . .

Example 2

Remove an element from the given index in a single-dimensional array.

using System;

namespace ConsoleApplication1 {
  class Program {
    static void Main() {
      int i = 0;
      int pos = 0;
      
      int[] arr = new int[10];

      //Read numbers into array
      Console.WriteLine("Enter numbers : ");
      for (i = 0; i < 5; i++) {
        Console.Write("Element[" + (i + 1) + "]: ");
        arr[i] = int.Parse(Console.ReadLine());
      }

      Console.Write("Enter position to delete item : ");
      pos = int.Parse(Console.ReadLine());

      //Perform shift opearation
      for (i = pos - 1; i < 5; i++) {
        arr[i] = arr[i + 1];
      }

      //print array after deletion
      Console.WriteLine("Array elements after deletion : ");
      for (i = 0; i < 4; i++) {
        Console.WriteLine("Element[" + (i + 1) + "]: " + arr[i]);
      }

      Console.WriteLine();
    }
  }
}

Output

Enter numbers :
Element[1]: 10
Element[2]: 20
Element[3]: 30
Element[4]: 40
Element[5]: 50
Enter position to delete item : 2
Array elements after deletion :
Element[1]: 10
Element[2]: 30
Element[3]: 40
Element[4]: 50



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.