Declaring a fixed size array in C#

C# | declaring a fixed size array: Here, we are going to learn how to declare, initialize, input and print a fixed size array in C#? By IncludeHelp Last updated : April 11, 2023

Prerequisite: C# Single-dimensional arrays

Fixed size array in C#

In C#, an array can be declared with a fixed size length and a variable length. Sometimes it's required that we need to have an array of fixed length. In this tutorial, we will learn how to declare an array of fixed size length, how to declare an array with a variable length i.e. input array length at run time, how to print array length (using Array.Length property), how to input array elements at run time and how to print array elements?

Syntax to declare a fixed size array

<data_type>[] array_name = new <data_type>[size];

Here is the declaration statement for an integer type 10 elements array,

int[] arr = new int[10];

When an integer array is declared it initialized with default values (0), if we print array elements, it will print zeros.

C# example to declare a fixed size array, input and print array elements

using System;

namespace Test {
  class Program {
    static void Main(string[] args) {
      //declaring a 10 elements integer array
      int[] arr = new int[10];

      //printing size
      Console.WriteLine("Array size is: " + arr.Length);

      //printing array 
      Console.WriteLine("Array elements...");
      foreach(int item in arr) {
        Console.Write(item + " ");
      }
      Console.WriteLine();

      //input and assigning elements to the array
      for (int i = 0; i < arr.Length; i++) {
        Console.Write("Enter " + i + " element: ");
        arr[i] = Convert.ToInt32(Console.ReadLine());
      }

      //printing array 
      Console.WriteLine("Array elements...");
      foreach(int item in arr) {
        Console.Write(item + " ");
      }
      Console.WriteLine();

      //hit ENTER to exit
      Console.ReadLine();
    }
  }
}

Output

Array size is: 10
Array elements...
0 0 0 0 0 0 0 0 0 0
Enter 0 element: 10
Enter 1 element: 20
Enter 2 element: 30
Enter 3 element: 40
Enter 4 element: 50
Enter 5 element: 60
Enter 6 element: 60
Enter 7 element: 80
Enter 8 element: 90
Enter 9 element: 55
Array elements...
10 20 30 40 50 60 60 80 90 55

Input array size at run time and declare an array

Here, we will input array length and will declare an array of variable length (according to input value).

Syntax

int[] arr = new int[len];

Here, len is a variable, that will be read from the user.

C# example to Input length and declare an array at run time, input and print array elements

using System;

namespace Test {
  class Program {
    static void Main(string[] args) {
      //variable to store length
      int len = 0;

      //input array length
      Console.Write("Enter array length: ");
      len = Convert.ToInt32(Console.ReadLine());

      //declaring array of len size at run time
      int[] arr = new int[len];

      //printing size
      Console.WriteLine("Array size is: " + arr.Length);

      //printing array 
      Console.WriteLine("Array elements...");
      foreach(int item in arr) {
        Console.Write(item + " ");
      }
      Console.WriteLine();

      //input and assigning elements to the array
      for (int i = 0; i < arr.Length; i++) {
        Console.Write("Enter " + i + " element: ");
        arr[i] = Convert.ToInt32(Console.ReadLine());
      }

      //printing array 
      Console.WriteLine("Array elements...");
      foreach(int item in arr) {
        Console.Write(item + " ");
      }
      Console.WriteLine();

      //hit ENTER to exit
      Console.ReadLine();
    }
  }
}

Output

Enter array length: 5
Array size is: 5
Array elements...
0 0 0 0 0
Enter 0 element: 100
Enter 1 element: 200
Enter 2 element: 300
Enter 3 element: 400
Enter 4 element: 500
Array elements...
100 200 300 400 500



Comments and Discussions!

Load comments ↻





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