By 22/07/2015 9 Comments

Implicitly Typed Arrays In C#

  • Implicitly typed arrays in C# have not any specific data type.
  • When we initialize these type of arrays with any data type, the compiler convert these arrays into that data type at compile time.
  • These types of arrays are being declared by using a keyword var, we cannot use square brackets [ ] with var keyword.
  • Implicitly typed arrays must be initialized and have same or compatible data types.
  • In C# all array types (single dimensional, 2D and jagged array) can be created as implicitly typed arrays.

Example – 1, implicitly typed single dimensional array:

using System;
namespace csharpBasic
{
    class Program
    {
        static void Main(string[] args)
        {
            // One- dimensional implicitly typed array initialization.
            var programingLanguages = new[] { "C#", "Php", "Asp.net" };

            // foreach loops through each array element.
            foreach (string displayElements in programingLanguages)

                Console.WriteLine(displayElements);

            Console.ReadKey();
        }
        /*
        The Output will be:
        
        C#
        Php
        Asp.net
       */
    }
}


Example – 2, implicitly typed multidimensional (2D) array:

using System;
namespace csharpBasic
{
    class Program
    {
        static void Main(string[] args)
        {
            // Sting type Multidimensional 2D implicitly typed array initialization.
            var programingLanguages = new[,] { { "C#", "Php", "Asp.net" }, { "Javascript", "Css3", "Html5" } };

            // An outer for loops through each rows of 2D array.
            for (int rows = 0; rows < 2; rows++)

                // An inner for loops through each columns of 2D Array.
                for (int cols = 0; cols < 3; cols++)

                    Console.WriteLine(programingLanguages[rows, cols]);

            Console.ReadKey();
        }
        /*
        The Output will be:
        
        C#
        Php
        Asp.net
        Javascript
        Css3
        Html5
       */
    }
}


Example – 3, implicitly typed jagged array:

using System;
namespace csharpBasic
{
    class Program
    {
        static void Main(string[] args)
        {
           // Implicitly typed jagged array initialization.
            var studentsRollNumbers = new[] { 
                new[] { 1, 2, 3 }, 
                new[] { 4, 5, 6},
                new[] { 7, 8 ,9}
            };

            // An outer loop which iterates each jagged array’s index.
            for (int jaggedIndex = 0; jaggedIndex < studentsRollNumbers.Length; jaggedIndex++)

                // An inner loop which iterates each inner/nested array’s index.
                for (int innerIndex = 0; innerIndex < studentsRollNumbers[jaggedIndex].Length; innerIndex++)
                
                    Console.Write(",{0}",studentsRollNumbers[jaggedIndex][innerIndex]);

            Console.ReadKey();
        }
        /*
        The Output will be:
        
        ,1,2,3,4,5,6,7,8,9
       */
    }
}

REMEMBER:

  • With implicitly typed arrays you must use new [ ] syntax in all array types.





Next Tutorial →


← Previous Tutorial


Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .