C# | Implicitly Typed Arrays - GeeksforGeeks

23 Jun, 2021
Implicitly typed arrays are those arrays in which the type of the array is deduced from the element specified in the array initializer. The implicitly typed arrays are similar to implicitly typed variable. In general, implicitly typed arrays are used in the query expression.Important points about implicitly typed arrays:  
In C#, the implicitly typed arrays do not contain any specific data type.
In implicitly typed array, when the user initializes the arrays with any data type then compiler automatically convert these arrays into that data type.
Implicitly typed arrays generally declared using var keyword, here var does not follow []. For Example: 
var iarray = new []{1, 2, 3};
All types of array-like 1-D, Multidimensional, and Jagged Arrays etc. can be created as an implicitly typed array.
In C#, it is necessary to initialize implicitly typed array and have the same data type.
Example 1: Below program illustrates how to use 1-Dimensional Implicitly typed array.  
CSharp
// C# program to illustrate// 1-D implicitly typed arrayusing System; public class GFG {     // Main method    static public void Main()    {         // Creating and initializing 1-D        // implicitly typed array        var author_names = new[] {"Shilpa", "Soniya",                                  "Shivi", "Ritika"};         Console.WriteLine("List of Authors is: ");         // Display the data of the given array        foreach(string data in author_names)        {            Console.WriteLine(data);        }    }}
List of Authors is: 
Shilpa
Soniya
Shivi
Ritika
 
Example 2: Below program illustrate the use of Multidimensional implicitly typed arrays.  
CSharp
// C# program to illustrate// 2-D implicitly typed arrayusing System; public class GFG {     // Main method    static public void Main()    {         // Creating and initializing        // 2-D implicitly typed array        var language = new[, ] { {"C", "Java"},                            {"Python", "C#"} };         Console.WriteLine("Programming Languages: ");          // taking a string        string a;         // Display the value at index [1, 0]        a = language[1, 0];        Console.WriteLine(a);         // Display the value at index [0, 2]        a = language[0, 1];        Console.WriteLine(a);    }}
Programming Languages: 
Python
Java
 
Example 3: Below code demonstrate the use of Implicitly typed jagged arrays. 
CSharp
// C# program to illustrate// implicitly typed jagged arrayusing System; class GFG {     // Main method    static public void Main()    {         // Creating and initializing        // implicitly typed jagged array        var jarray = new[] {            new[] { 785, 721, 344, 123 },            new[] { 234, 600 },            new[] { 34, 545, 808 },            new[] { 200, 220 }        };         Console.WriteLine("Data of jagged array is :");         // Display the data of array        for (int a = 0; a < jarray.Length; a++) {            for (int b = 0; b < jarray[a].Length; b++)                Console.Write(jarray[a][b] + " ");            Console.WriteLine();        }    }}
Data of jagged array is :
785 721 344 123 
234 600 
34 545 808 
200 220
 
ruhelaa48
CSharp-Arrays
Picked
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Extension Method in C#
HashSet in C# with Examples
C# | Inheritance
Partial Classes in C#
C# | Generics - Introduction
Top 50 C# Interview Questions & Answers
Switch Statement in C#
Convert String to Character Array in C#
C# | How to insert an element in an Array?
Linked List Implementation in C#