Loading...

How to Implicitly Typed Arrays In C#

View: 794    Dowload: 0   Comment: 0   Post by: hanhga   Category: C / C++ / MFC   Fields: Other

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
       */
    }
}

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

 

How to Implicitly Typed Arrays In C#

When we initialize these type of arrays with any data type, the compiler convert these arrays into that data type at compile time.

Posted on 12-09-2016 

Comment:

To comment you must be logged in members.

Files with category

  • Sound Player in Visual Studio C++ .NET

    Sound Player in Visual Studio C++ .NET

    View: 13    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    This sound player project use the libZPlay from http://libzplay.sourceforge.net . Methods are called from library libzplay.dll with [InteropServices::DllImport("")]. The use of the library is relatively easy.

  • Quiz Management System in C++

    Quiz Management System in C++

    View: 19    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    Hello World...!!! Though I am not an expert programmer but today i want to share my little knowledge of C/C++ programming by sharing this simple Quiz Management System with you people. I hope this may help some students in their mini...

  • General Quiz

    General Quiz

    View: 104    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    This is one of my first WPF projects, and it may be used as a template for a quiz test. The code can be improved but I think its useful for the ones who don't have experience with WPF.

  • Calculating Average and Grade

    Calculating Average and Grade

    View: 34    Download: 2   Comment: 0

    Category: C / C++ / MFC     Fields: none

    Calculating Average and Grade is a small program in order to find the result of students in the class.

  • H.K.M.S SUPREME TYRES

    H.K.M.S SUPREME TYRES

    View: 25    Download: 1   Comment: 0

    Category: C / C++ / MFC     Fields: none

    H.K.M.S SUPREME TYRES --------------------- Goals: --------- * Brand Master * Size Master * User Master * Customer Master * Purchase Form * Order Form * Bill Form Software is as follows: ------------------------------ * Microsoft Visual Studio 2013 *...

  • Cronpp - a C++ library for CRON expressions

    Cronpp - a C++ library for CRON expressions

    View: 18    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: Other

    A CRON expression is a string composed of six fields (in some implementation seven), separated by a whites space, representing a time schedule. CRON expressions are used in various job schedulers (such as the Linux job scheduler, the Quartz...

  • Departmental store management system

    Departmental store management system

    View: 25    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    This is a simple project on departmental store management system developed in c++ programming language . This project helps to keep all the information details of the items available in departmental store and maintains the stock and prints the bill....

  • Volume of a Cone in C

    Volume of a Cone in C

    View: 37    Download: 0   Comment: 0

    Category: C / C++ / MFC     Fields: none

    Code Features: 1. Find the Volume of a cone 2. Find the value of other properties of the cone 3. Teach user how to compute the volume of the cone manually Thanks to following for the code: John Carlo C. Nañoz, Dondon Bagasbas, Gabby Peñones

 
File suggestion for you
Loading...
File top downloads
Loading...
Loading...
Codetitle - library source code to share, download the file to the community
Copyright © 2018. All rights reserved. codetitle Develope by Vinagon .Ltd