Q:

C# | different types of one dimensional array declarations

belongs to collection: C# Basic Programs | array programs

0

1) One dimensional Array declaration with initialization (without array size)

Example:

    int[] arr1 = { 12, 45, 56, 78, 89 };

In this style, arr1 will automatic occupy the size of the array based on given elements.

2) Dynamic One dimensional Array declaration with fixed size

Example:

    int[] arr2 = new int[5];

In this style, arr2 will occupy the size for 5 integers dynamically.

3) Dynamic One dimensional Array declaration with variable size

Example:

    int[] arr3 = new int[size];

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Arrays
{
    class Program
    {
        static void Main(string[] args)
        {
            //Single Dimension

            Console.WriteLine("Type 1 : Declaration");
            int[] arr1 = { 12, 45, 56, 78, 89 };
            foreach(int item in arr1)
            {
                Console.Write("{0}\t", item);
            }


            Console.WriteLine("\n\nType 2 : Declaration");
            int[] arr2 = new int[5];
            Console.WriteLine("Enter 5 Values:");
            for(int i=0;i<arr2.Length;i++)
            {
                arr2[i] = Convert.ToInt32(Console.ReadLine());
            }
            foreach (int item in arr2)
            {
                Console.Write("{0}\t", item);
            }

            Console.WriteLine("\n\nType 3 : Declaration");
            Console.Write("Enter Size:");
            int size = Convert.ToInt32(Console.ReadLine());
            int[] arr3 = new int[size];
            Console.WriteLine("Enter {0} Values:",size);
            for (int i = 0; i < arr3.Length; i++)
            {
                arr3[i] = Convert.ToInt32(Console.ReadLine());
            }
            foreach (int item in arr3)
            {
                Console.Write("{0}\t", item);
            }

            Console.ReadKey();
        }
    }
}

Output

Type 1 : Declaration
12      45      56      78      89

Type 2 : Declaration
Enter 5 Values:
10
20
30
40
50
10      20      30      40      50

Type 3 : Declaration
Enter Size:3
Enter 3 Values:
100
200
300
100     200     300

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | array programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# | printing an integer array using foreach loop... >>