How to Implicitly Typed Arrays In C#

View: 751    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

  • C# - Simple Phone Directory System

    C# - Simple Phone Directory System

    View: 6    Download: 0   Comment: 0

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

    Simple Phone Directory System Please make scan first using your antivirus. thanks.

  • School Management System

    School Management System

    View: 12    Download: 1   Comment: 0

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

    his School Management Software is Best School Management System written in C#.Net 4.0 as front end and SQL Server 2008as Back end.We can also manage Multiple branches of a School by This Software ... Requirement : Visual Studio 2010/2012/2013/2015...

  • C and C++ SDSSU compilation

    C and C++ SDSSU compilation

    View: 33    Download: 0   Comment: 0

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

    This is the compilation of my 1st year output in C and C++ programming languages. It contains some basic activity like addition, subtraction, multiplication, division and other educational information that can improve and enhance the skills of our...

  • Enrollment and Billing System in C#

    Enrollment and Billing System in C#

    View: 28    Download: 2   Comment: 0

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

    Enrollment and Billing System This is a project entitled Enrollment and Billing System written in C# and SQL Server Management Studio R2 2008 database for Christian Kiddie Care. The system has the following features: Transactions Add Student Add...

  • SqlCommand to T-SQL Printer

    SqlCommand to T-SQL Printer

    View: 291    Download: 0   Comment: 0

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

    In this article, we are going to see a log printer working with Entity Framework that converts SqlCommand to a completely executable T-SQL statement, including all parameter declarations and write to log file. Once a command is executed, You will see...

  • Windows Clipboard Formats

    Windows Clipboard Formats

    View: 123    Download: 0   Comment: 0

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

    This reference gives you an overview of standard and common clipboard formats with links to the data structures.

  • Insert 1 element at the beginning of an array managed by a pointer without moving

    Insert 1 element at the beginning of an array managed by a pointer without moving

    View: 102    Download: 0   Comment: 0

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

    Insert 1 element at the beginning of an array managed by a pointer without moving

  • An autonomous and concealed IPC mechanism

    An autonomous and concealed IPC mechanism

    View: 189    Download: 0   Comment: 0

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

    What if you have one or more modules, running independently in a system, which should collaborate between each other? Well, they will have to be able to exchange data, organized as events, messages or packets of some sort.

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