Home » C#.Net

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#?
Submitted by IncludeHelp, on March 30, 2019

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

Example:

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.

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

using System;
using System.Text;
using System.Collections;

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.

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

using System;
using System.Text;
using System.Collections;

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

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.