C# | Implicitly Typed Arrays

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 illustrate how to use 1-Dimesional Implicitlt typed array.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate
// 1-D implicitly typed array
using 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);
        }
    }
}

chevron_right


Output:

List of Authors is: 
Shilpa
Soniya
Shivi
Ritika

Example 2: Below program illustrate the use of Multidimensional implicitly typed arrays.



filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate
// 2-D implicitly typed array
using 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);
    }
}

chevron_right


Output:

Programming Languages: 
Python
Java

Example 3: Below code demonstrate the use of Implicitly typed jagged arrays.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate
// implicitly typed jagged array
using 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();
        }
    }
}

chevron_right


Output:

Data of jagged array is :
785 721 344 123 
234 600 
34 545 808 
200 220


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.