Home » C#.Net

Implicitly Typed Arrays in C#

C# implicitly typed arrays: Here, we are going to learn about the implicitly typed arrays in C#, how to declare an implicitly typed array in C#?
Submitted by IncludeHelp, on March 11, 2019

C# Implicitly Typed Arrays

Like implicitly typed variables, we can also declare an array without specifying its type such type of arrays are known as Implicitly typed arrays.

The type of the array is determined by the compiler based on the initializer list.

Syntax:

    var array_name = new[] {initialize_list/elements};

Example:

    var arr1 = new[] { 10, 20, 30, 40, 50 };
    var arr2 = new[] { 10.0f, 20.1f, 30.2f, 40.3f, 50.4f };
    var arr3 = new[] { "Manju", "Amit", "Abhi", "Radib", "Prem" };

Here, arr1 will be determined as int[] (integer array), arr2 as float[] (float/single array), and arr3 as String[] (string array).

C# code to demonstrate example of implicitly typed arrays

using System;
using System.Text;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            var arr1 = new[] { 10, 20, 30, 40, 50 };
            var arr2 = new[] { 10.0f, 20.1f, 30.2f, 40.3f, 50.4f };
            var arr3 = new[] { "Manju", "Amit", "Abhi", "Radib", "Prem" };

            //printing type of the array
            Console.WriteLine("Type of arr1: " + arr1.GetType());
            Console.WriteLine("Type of arr2: " + arr2.GetType());
            Console.WriteLine("Type of arr3: " + arr3.GetType());

            //printing the elements
            Console.WriteLine("arr1 elements...");
            foreach (var item in arr1)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();
            Console.WriteLine("arr2 elements...");
            foreach (var item in arr2)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();

            Console.WriteLine("arr3 elements...");
            foreach (var item in arr3)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();

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

Output

Type of arr1: System.Int32[]
Type of arr2: System.Single[]
Type of arr3: System.String[]
arr1 elements...
10 20 30 40 50
arr2 elements...
10 20.1 30.2 40.3 50.4
arr3 elements...
Manju Amit Abhi Radib Prem





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







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.